php - jQuery AJAX call failing until after refresh -


so i've asked similar question here before , got answer worked, however, after progressing in beginner project , introduction ajax, i've come across problem. seems ajax call isn't successful..until after page reloaded. noticed issue prior adding more text fields form, can't imagine how related. later query reliant on whether form submitted, don't want make user refresh page data pushed through database. suggestions?

    <form action="call.php" method="post">     <input class="oomtext" type="text" name="accountname" placeholder="account name"><br>     <h2 class="oomhead2">email notifcations phone calls</h2>     <input class="oomtext" type="text" name="accountemailone" placeholder="email recipient 1*"><br>     <input class="oomtext" type="text" name="accountemailtwo" placeholder="email recipient 2*"><br>     <input class="oomtext" type="text" name="accountemailthree" placeholder="email recipient 3*"><br>     <h2 class="oomhead2">text notifcations phone calls</h2>     <input class="oomtext" type="text" name="accounttextone" placeholder="text recipient 1*"><br>     <input class="oomtext" type="text" name="accounttexttwo" placeholder="text recipient 2*"><br>     <input class="oomtext" type="text" name="accounttextthree" placeholder="text recipient 3*"><br>     <small>*fields <b>not</b> required filled out</small><br>     <input class="oombutton submit" type="submit" name="submit" value="submit">     <script>         $(document).ready(function(){             $('.oombutton.submit').click(function(){                 var clickbtnvalue = $(this).val();                 var ajaxurl = 'http://clients.oomdo.com/provision/ajax.php',                 data =  {'action': clickbtnvalue};                 $.post(ajaxurl, data, function (response) {                  alert("account created");                 });             });         });     </script>     </form> 

..later on in php file

  if ($_session['submitted'] == true){    //     } 

ajax.php

    <?php         session_start();         if (isset($_post['action'])) {             switch ($_post['action']) {                 case 'submit':                     submit();                     echo 'hurrah';             }         }          function submit() {             $_session['submitted'] = true;         }      ?> 

i'm little rusty bare me, think you've forgotten ajax asynchronous, meaning happens in there doesn't happen until later.

so, in example, you've done ajax call, checked session, call finished function (taking longer because of higher amount of data sent). that's why sees new value after refresh.

you have 3 options here:

  1. include //do something statement success function.

code sample:

$.post(ajaxurl, data, function (response) {     alert("account created");      if ($_session['submitted'] == true){         //     } }); 
  1. create function , call @ end of success function.

code sample:

function sessioncheck(){     if ($_session['submitted'] == true){         //     } }  ....  $.post(ajaxurl, data, function (response) {     alert("account created");      sessioncheck(); }); 
  1. give $(document) .ajaxsuccess(function). trigger, each time have ajax call completed. do note multiple instances of may overwrite them in case, you'll have add identifying session variable each .php output.

code sample

$(document).ajaxsuccess(function() {      if ($_session['submitted'] == true){         //            //then find way remove $_session['submitted']         //        maybe using ajax well.     }      //else if ($_session['somethingelse'] == true) }); 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -