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 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)