Update PHP Variable after Ajax call -


i making ajax call php file checks condition , runs query mysql query if conditions met.

the query updates table in db new value. works great. know how show new value in current page without having manually reload. code below.

the variable updating $trialexpiry

html/php

<h4 class="sbtext mt-10">trial end date: <?php echo date("d/m/y",                         strtotime($trialexpiry)); ?></h4>  <form id='promocode'>      <input type='text' class='sbinput' placeholder='promo code' name='promocode'>      <input type='hidden' name='userid' value='<?php echo $userid; ?>'>      <button class='btn sbsubmit'>submit</button> </form> 

jquery

   <script>          $(function () {              $('#promocode').on('submit', function (e) {                   e.preventdefault();                   $.ajax({                      type: 'post',                      url: '../model/process-promo-code.php',                      data: $('#promocode').serialize(),                      success: function () {                           $("button.btn").css({                              'transition-duration': '1000ms',                               'opacity': '0.5'                            });                      }                  });             });       });    </script> 

thanks much.

you don't want live updating php variables, since refreshed when page reloaded. instead, want update element's value via ajax. far can tell, want update expiration date. if don't, let me know , can change code whatever it's supposed do.

here's "control flow" of functionality:

  1. (entry point) user clicks 'submit', jquery event handler fires
  2. jquery ajax function called , sends promo code php script
  3. in php script, database updated promo code.
  4. the php script returns new expiry date (i'll assume it's in d/m/y format wanted)
  5. the callback in jquery ajax function receives data script.
  6. the callback's function body updates "expiry" element on page value php call.

here's how put html / javascript:

<h4 class="sbtext mt-10" id="expiry_label">     trial end date: <?php echo date("d/m/y",                     strtotime($trialexpiry)); // initial value can displayed normal. ?> </h4>  <form id='promocode'>      <input type='text' class='sbinput' placeholder='promo code' name='promocode'>      <input type='hidden' name='userid' value='<?php echo $userid; ?>'>      <button class='btn sbsubmit'>submit</button> </form>   <script>      $(function () {          $('#promocode').on('submit', function (e) {               e.preventdefault();               $.ajax({                  type: 'post',                  url: '../model/process-promo-code.php',                  data: $('#promocode').serialize(),                  success: function (result) {                       $("button.btn").css({                          'transition-duration': '1000ms',                           'opacity': '0.5'                        });                        document.getelementbyid("expiry_label").innerhtml = "trial end date: " + result;                  }              });         });   }); </script> 

as can see, added "id" attribute element, parameter "success" property of ajax call, , line of code update element.

hope helped!


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -