dongtan6336 2017-08-14 12:08
浏览 64
已采纳

在Ajax调用之后更新PHP变量

I am making an Ajax call to a PHP file that checks a condition and runs a query a MySQL query if the conditions are met.

The query updates a table in my DB with a new value. This all works great. I would like to know how to show the new value in the current page without having to manually reload. Code is Below.

The variable I am updating is $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 so much.

展开全部

  • 写回答

3条回答 默认 最新

  • doubing3662 2017-08-14 12:26
    关注

    You don't want to do the live updating with PHP variables, since those are only refreshed when the page is reloaded. Instead, you want to update an element's value via AJAX. As far as I can tell, you want to update the expiration date. If you don't, just let me know and I can change the code to whatever it's supposed to do.

    Here's the "control flow" of this functionality:

    1. (Entry point) User clicks 'Submit', jQuery event handler fires
    2. jQuery AJAX function is called and sends the promo code to a PHP script
    3. In the PHP script, the database is updated with the promo code.
    4. The PHP script returns the new expiry date (I'll assume that it's in the d/m/Y format you wanted)
    5. The callback in the jQuery AJAX function receives the data from the script.
    6. The callback's function body updates the "Expiry" element on the page with the value from the PHP call.

    Here's how to put that into HTML / JavaScript:

    <h4 class="sbText mt-10" id="expiry_label">
        Trial End Date: <?php echo date("d/m/Y",
                        strtotime($trialExpiry)); // The initial value can be displayed as 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 you can see, I only added an "id" attribute to the element, a parameter to the "success" property of the AJAX call, and a line of code to update the element.

    Hope I helped!

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
  • ¥15 IEC61850 客户端和服务端的通讯机制
  • ¥15 MAX98357a(关键词-播放音频)
  • ¥15 Linux误删文件,请求帮助
  • ¥15 IBMP550小型机使用串口登录操作系统
  • ¥15 关于#python#的问题:现已知七自由度机器人的DH参数,利用DH参数求解机器人的逆运动学解目前使用的PSO算法
  • ¥15 发那科机器人与设备通讯配置
  • ¥15 Linux环境下openssl报错
  • ¥15 我在使用VS编译并执行之后,但是exe程序会报“无法定位程序输入点_kmpc_end_masked于动态链接库exe上“,请问这个问题有什么解决办法吗
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部