duanchuo7741 2016-12-31 19:32
浏览 39
已采纳

Ajax正在发送数据,但Php文件没有收到它

I've spent the past 10 or so days trying to fix this to some success but I'm still stuck. I'm making an online budgeting system which takes Javascript values, sends them via Ajax to PHP where they are then sent to a database. Here is my code for javascript

$.ajax({
   url: 'yup.php',
   type:'post',
   data:{
          'Sal': Sal,
          'TaxReb': TaxReb,
          'RetInv': RetInv,
          'IntSave': IntSave,
          'Pen': Pen,
          'Dividends': Dividends,
          'Wages':Wages,
          'Loans':Loans,
          'Benefits':Benefits,
          'IncOther': IncOther,
          'Tax': Tax ,
          'Groc': Groc ,
          'Take':Take ,
          'Rent': Rent ,
          'Elec': Elec ,
          'Trans': Trans ,
          'Mort': Mort ,
          'LoanPay':LoanPay ,
          'Goods': Goods ,
          'ExpOther': ExpOther ,
          'TotalInc':TotalInc ,
          'TotalExp': TotalExp ,
          'NetInc':NetInc
    },

    success: function(data){
        alert(data);
        window.location.href='yup.php';
    }
});

The values are integers which are working on the Jscript file. The alert gives me this message:

Array(
[Sal] => 33245
[TaxReb] => 324523
[RetInv] => 3245
[IntSave] => 2345
[Pen] => 3524
[Dividends] => 5342
[Wages] => 35425342
[Loans] => 5432
[Benefits] => 254325
[IncOther] => 4352
[Tax] => 34
[Groc] => 3434
[Take] => 34
[Rent] => 34
[Elec] => 34
[Trans] => 34
[Mort] => 34
[LoanPay] => 34
[Goods] => 34
[ExpOther] => 34
[TotalInc] => 36061675
[TotalExp] => 3740
[NetInc] => 36057935
)`

My PHP code is this:

<?php header(' charset=utf-8');
print_r($_POST)
//$Sal=$_POST['Sal'];
//echo $Sal;

However on my PHP page it gives me this: Array ( ) And when I uncomment the variables it gives me the error:

Notice: Undefined index: Sal in C:\xampp\htdocs\example\yup.php on line 2

I removed the print_r when trying to get the actual variable. When I use server request method the alert says post whereas the php page says get. This might be part of the problem but I have no idea how to fix it and neither did the other pages I looked at on this or other websites. All help is appreciated.

  • 写回答

1条回答 默认 最新

  • duanmi3476 2016-12-31 22:24
    关注

    You are making two separate requests to yup.php. There is the POST request made using AJAX and there is the GET request made when you redirect the user to yup.php in the success() function. The GET request, which is what is sending information back to the user to display on the page, doesn't receive the information from the AJAX request because, well, you didn't send the server any information in the GET request. You previously made a separate POST request to yup.php which did receive the data and proceeded to run your script (i.e. print out the information).

    If your goal is to present the user some response from the server after submitting the AJAX request, then you should use the data argument in your success() function to do so. What you've done with the alert() is one way to present the data, though likely unsatisfactory. What you likely want to do is use JavaScript to insert that data somewhere into the current page.

    The crucial point to understand here is that you are making two separate requests to yup.php, only one of which receives the data you sent using AJAX.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上