Search code examples
phpjqueryajaxpostsubmission

jQuery AJAX post submission to a PHP script multiple times


I am trying to do a form submission without having the page reload. It works the first time, but when I try and do a second submission the page is refreshed.

I would like to be able to do several submissions with the new data propagating into the current element. What am I missing.

Here is my first page form.php

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
  <html>
    <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>

<script type="text/javascript">
    //<![CDATA[
    jQuery.noConflict();
    jQuery(document).ready(function () {
        jQuery("#form").validate({
            debug: false,
            submitHandler: function (form) {
                jQuery.post('formPost.php',
                    jQuery("#form").serialize(), function (data) {
                    jQuery('#Box').html(data);
                });
            }
        });
    });
    // ]]>
</script>

  </head>
  <body>
    <div id="Box">
      <form id="form" name="form" method="POST" action="">
        <select name="color">
          <option>red</option>
          <option>white</option>
          <option>blue</option>
        </select>
        <input type="submit" name="submit" value="submit" /><br />
      </form>
    </div>
  </body>
</html>

and here is my second page formPost.php

<?php

  switch ($_POST['color'])
  {
    case 'red':
      echo 'you chose red<br /><br />';
      break;

    case 'blue':
      echo 'you chose blue<br /><br />';
      break;

    case 'white':
      echo 'you chose white<br /><br />';
      break;
  }
?>
<form id="form" name="form" method="POST" action="">
  <select name="color">
    <option>red</option>
    <option>white</option>
    <option>blue</option>
  </select>
  <input type="submit" name="submit" value="submit" /><br />
</form>

I want to be able to reuse the same form for multiple form submissions while staying on the same page and having the data apprear in the same div without refreshing the page. Any ideas


Solution

  • Try this:

    <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
        <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
        <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
    
    <script type="text/javascript">
        //<![CDATA[
        jQuery.noConflict();
        jQuery(document).ready(function () {
            initializeForm();
        });
    
        function initializeForm() {
            jQuery("#form").validate({
                debug: false,
                submitHandler: function (form) {
                    jQuery.post('formPost.php',
                        jQuery("#form").serialize(), function (data) {
                        jQuery('#Box').html(data);
                        initializeForm();
                    });
                }
    
            });
        }
    
        // ]]>
    </script>
    
      </head>
      <body>
        <div id="Box">
          <form id="form" name="form" method="POST" action="">
            <select name="color">
              <option>red</option>
              <option>white</option>
              <option>blue</option>
            </select>
            <input type="submit" name="submit" value="submit" /><br />
          </form>
        </div>
      </body>
    </html>
    

    I tested this out and it works locally for me. I hope this helps!