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条)

报告相同问题?

悬赏问题

  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了