简体   繁体   English

Ajax正在发送数据,但Php文件未接收到数据

[英]Ajax is sending data but Php file is not recieving it

I've spent the past 10 or so days trying to fix this to some success but I'm still stuck. 我花了过去10天左右的时间来尝试解决此问题,但仍无法解决。 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. 我正在制作一个在线预算系统,该系统将Javascript值通过Ajax发送到PHP,然后将其发送到数据库。 Here is my code for javascript 这是我的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. 这些值是在Jscript文件上运行的整数。 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代码是这样的:

<?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: 但是在我的PHP页面上,它给了我这个:Array()当我取消注释变量时,它给了我错误:

Notice: Undefined index: Sal in C:\\xampp\\htdocs\\example\\yup.php on line 2 注意:未定义的索引:第2行的C:\\ xampp \\ htdocs \\ example \\ yup.php中的Sal

I removed the print_r when trying to get the actual variable. 尝试获取实际变量时,我删除了print_r When I use server request method the alert says post whereas the php page says get . 当我使用服务器请求方法时 ,警报显示post,而php页面显示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. 感谢所有帮助。

You are making two separate requests to yup.php . 您正在向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. 当您使用success()函数将用户重定向到yup.php时,有使用AJAX进行的POST请求,也有GET请求。 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. GET请求将信息发送回用户以显示在页面上,但它没有从AJAX请求中接收信息,因为您没有在GET请求中向服务器发送任何信息。 You previously made a separate POST request to yup.php which did receive the data and proceeded to run your script (ie print out the information). 您之前yup.php发出了单独的POST请求,该请求确实接收到数据并继续运行脚本(即打印出信息)。

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. 如果您的目标是在提交AJAX请求后向用户呈现服务器的某些响应,则应在success()函数中使用data参数来这样做。 What you've done with the alert() is one way to present the data, though likely unsatisfactory. 您使用alert()所做的事情是呈现数据的一种方法,尽管可能并不令人满意。 What you likely want to do is use JavaScript to insert that data somewhere into the current page. 您可能想要做的是使用JavaScript将数据插入当前页面的某处。

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. 这里要理解的关键点是,您要向yup.php发出两个单独的请求, yup.php只有一个接收使用AJAX发送的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM