donglun4682 2017-11-21 21:15
浏览 33
已采纳

来自PHP文件中的jQuery的GET参数没有带来?

I have some data in an ajax request that I am sending to a php file as an endpoint.

The code is the following:

// some calculations above here, but to keep things minimal assume they produce the following
totalBeforeTip = 38.43;
tipTotal = 5.77;
totalWithTip = 44.48;
saleCount = "3";
  console.log("List for the query String: " + totalBeforeTip 
                                        + "
 Tips: " + tipTotal 
                                        + " 
 Totals+Tip: "+ totalWithTip
                                        + "
 Totals Sales: " + saleCount);

  // Send get to php with q-string

  $.ajax({
    url:"summary.php",
    type: "get",
    data:{
      TotalSales: saleCount, 
      TotalsNoTip:totalBeforeTip, 
      TotalTip: tipTotal, 
      TotalWithTip: totalWithTip
    },
    success: function(resp){
      console.log(resp);
      window.location.href = "summary.php";
    },
    error: function(err){
      console.log(err);
    }
  });

And my php is the following:

<main>
    <?php
    $TotalSales = $_GET["TotalSales"];
    $TotalTips =  $_GET["TotalTip"];
    $TotalBeforeTip = $_GET["TotalsNoTip"];
    $TotalWithTip = $_GET["TotalWithTip"];
    ?>
 <div class='container'>
        <div class='row'>
            <div class='col-md-6'>
                  <form> <!--Main form for user interaction-->
                    <div class='form-group bigger-group'>
                      <label>Sales: </label>
                      <input readonly value="<?php echo $TotalSales  ?>" />
                    </div>
                      <div class='form-group'>
                          <label>Totals Before Tips:</label>
                      </div>
                      <div class='form-group'>
                          <label>Total Tip Cost:</label>
                      </div>
                      <div class='form-group'>
                          <label>Total Cost with Tip:</label>
                      </div>
                    </div>
                    <div class=col-md-6>
                    <h1>Cost ($)</h1>
                <div class='form-group'>
                    <input readonly value="<?php echo $TotalBeforeTip ?>"/>
                </div>
                <div class='form-group'>
                    <input readonly value="<?php echo $TotalTips ?>"/>
                </div>
                <div class='form-group'>
                    <input readonly value="<?php echo $TotalWithTip ?>"/>
                </div>
            </div>
        </div>

</main>

When I redirect my fields are blank but in the response in the console log they are filled. I tried to do a page redirect to the resp it doesn't do anything. After poking at it quite a bit I got it to work by adding the following line to success: function(resp){....}.

window.location = "summary.php?TotalSales="+saleCount+"&TotalsNoTip="+totalBeforeTip+"&TotalTip="+tipTotal+"&TotalWithTip="+totalWithTip;

For some reason this works, and the fields in the php have the proper data but I don't see how? Or why it is that the data I passed in with the .ajax method just evaporated?

I've been struggling with this for two days and it works but I don't understand why at all. Can anyone shed some light?

  • 写回答

2条回答 默认 最新

  • donglugou6652 2017-11-22 01:05
    关注

    So with the combination of all the feedback I've received and some more googling I've learned that AJAX is for ACTION not CONTENT and the core purpose of AJAX is to perform background processing without holding main flow on a page. Some good examples are :

    • Loading new tweets on twitter
    • Giving a notification if you have a new email in your inbox
    • Validating form fields on blur

    Since in my case I wanted to send the totalBeforeTip, tipTotal, totalWithTip, saleCount to my summary.php to load the content of the input fields based off that data. Using AJAX for it is a bad approach because this should be done in a synchronous process.

    I used the following code to accomplish this outside of ajax

    window.location = "summary.php?TotalSales="+saleCount+"&TotalsNoTip="+totalBeforeTip+"&TotalTip="+tipTotal+"&TotalWithTip="+totalWithTip;

    All I needed was the queryString.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度