javascript - Ajax form submitting to two scripts -


basically have single form want pass variables 2 separate php scripts processing on submit. have done works seems work fine purposes, i'm wondering if there way inside of 1 jquery script instead of having repeat script , changing url have done. or way iv gone fine? cant find many examples of usage on google maybe i'm not searching right things. if i'm doing bad practice please let me know, i'm new ajax , every tutorial follow seems things differently.

<script> $(document).ready(function(){     $('#sform').submit(function(){          // show loading         $('#response').html("<b>loading response...</b>");          /*          * 'post_receiver.php' - pass form data          * $(this).serialize() - read form data          * function(data){... - data contains response post_receiver.php          */         $.ajax({             type: 'post',             url: 'move_to_lease.php',              data: $(this).serialize()         })            .done(function(data){              // show response             $('#response').html(data);          })            .fail(function() {              // in case posting form failed             alert( "posting failed." );          });          // prevent refreshing whole page page         return false;      }); }); </script>  <script> $(document).ready(function(){     $('#sform').submit(function(){          // show loading         $('#txthint').html("<b>loading response...</b>");          /*          * 'post_receiver.php' - pass form data          * $(this).serialize() - read form data          * function(data){... - data contains response post_receiver.php          */         $.ajax({             type: 'post',             url: 'show_lease_sub.php',              data: $(this).serialize()         })         .done(function(data){              // show response             $('#txthint').html(data);          })         .fail(function() {              // in case posting form failed             alert( "posting failed." );          });          // prevent refreshing whole page page         return false;      }); });  </script> 

best practice handle both calls 1 back-end script. return 2 separate responses utilizing array. protip - write class.

ajax.php

class leasestuff{     public static function movetolease($data){         // add code move_to_lease.php, change $_post $data     }     public static function showleasesub($data){         // add code show_lease_sub.php, change $_post $data     } }  echo json_encode(array(     "movetolease"=>leasestuff::movetolease($_post),     "showleasesub"=>leasestuff::showleasesub($_post) )); 

then can consolidate ajax 1 call:

$.ajax({     type: 'post',     url: 'ajax.php',      data: $(this).serialize() }).done(function(data){     data = json.parse(data);     $('#response').html(data.movetolease);     $('#txthint').html(data.showleasesub); }); 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -