简体   繁体   English

$ _POST不反映使用jQuery post ajax发送的数据

[英]$_POST doesn't reflect data being sent with jQuery post ajax

I'm using ajax/json to send values to a php function that would then display the data on the page. 我正在使用ajax / json将值发送到php函数,该函数随后将在页面上显示数据。 I'm sending two values $hours and $memberID , except I seem to only be able to retrieve $hours and not $memberID . 我正在发送两个值$hours$memberID ,除了我似乎只能检索$hours而不是$memberID I ran the page through Firebug, on the javascript page the values for both variables are being read right before they're sent. 我在Firebug中运行了该页面,在javascript页面上,两个变量的值都在发送之前就已读取。 In the PHP form, I ran multiple conditions/echo statements to print out both values except only $hours is being displayed. 在PHP表单中,我运行了多个条件/回显语句以打印出两个值,但仅显示$hours Any ideas? 有任何想法吗?

PHP: PHP:

$result = array();
        if(!empty($_POST['hours'])) {
            $result['type'] = "success";
            $result['memberID'] = (int)$_POST['memID'];
            $result['hours'] = (int)$_POST['hours'];
            $result = json_encode($result);
            echo $result;
         }

Javascript: Javascript:

 //numberofMembers is the total number of entries in the database
function subtractHours(numberofMembers) {
document.body.style.cursor = "wait";
var hours = document.getElementById("hours");
var i = 1;
var studentID;
while(i < numberofMembers) {        
    studentID = document.getElementById("member"+i);    
    alert(studentID.value);
    if(studentID && studentID.checked) {            
        $.ajax({
            type : 'post',
            datatype: 'json',
            url : 'hours_subtract.php',
            data : {hours : hours.value, memID : studentID.value},
            success: function(response) {
                if(response == 'success') {
                    alert('Hours subtracted!');
                } else {
                    alert('Error!');
                }
            }
        }); 
        //$.post( "subtract.php", {person: personId, personSalary: personSalary} );
    }
    i++;
}   
}

PHP(HTML form): PHP(HTML格式):

echo "<input type='checkbox' name='member{$attNumber}' id='member{$attNumber}' value=$studentID/>$attendees<br />";

Edit: If I run a var_dump($_POST['memberID']); 编辑:如果我运行var_dump($_POST['memberID']); , it prints out NULL . ,它打印出NULL If I run a var_dump($_POST) , it prints out array(2) { ["hours"]=> string(1) "1" ["member3"]=> string(8) "5101813/" } . 如果我运行var_dump($_POST) ,它将打印出array(2) { ["hours"]=> string(1) "1" ["member3"]=> string(8) "5101813/" }

Edit: I added in the PHP code that uses member3 . 编辑:我添加了使用member3的PHP代码。 Edit 2: That's the entire Javascript function. 编辑2:这就是整个Javascript函数。 The PHP code is all that I have for processing the data. PHP代码是我处理数据所需的全部。

When using jQuery Ajax, if one of the data parameters being sent is null or undefined, it will be discarded, and not sent to the server. 使用jQuery Ajax时,如果要发送的数据参数之一为null或未定义,则将其丢弃,而不发送给服务器。 Since you aren't getting this property in your PHP (serverside), it's likely that it's not being set properly on the initiali client-side page. 由于您没有在PHP(服务器端)中获得此属性,因此很可能在初始客户端页面上未正确设置该属性。

memberID.value // check this value

Can you check that memberID.value is returning the expected value? 您可以检查memberID.value是否返回预期值?

I had a similar issue on my site. 我的网站上也有类似的问题。 It turns out my ajax request was not sending the data via the $_POST stream as I expected it would. 事实证明,我的ajax请求没有像我期望的那样通过$_POST流发送数据。 It was actually sending it through the request body. 它实际上是通过请求正文发送的。

To access it I had to do the following: 要访问它,我必须执行以下操作:

json_decode(file_get_contents(“php://input”));

This pulled the information from the request body and decoded the json data (which is the format I sent the data to the server) into something usable by PHP. 这从请求主体中获取信息,并将json数据(这是我将数据发送到服务器的格式)解码为PHP可以使用的格式。 Not sure but you may be experiencing a similar issue. 不确定,但是您可能遇到类似的问题。

The problem is that your example isn't really showing us the code you're using. 问题在于您的示例并未真正向我们显示您正在使用的代码。

The following duplicates your AJAX request which is the essence of this issue: 以下内容重复了您的AJAX请求,这是此问题的本质:

$.post(
    'hours_subtract.php',
    {
        hours : "test-val-1",
        memberID : "test-val-2"
    },
    function(resp) {
        // Handle the response
    }
);

There are only two values being sent to the server - hours and memberID 只有两个被发送到服务器的价值观- hoursmemberID

However, you state that var_dump($_POST) shows: 但是,您声明var_dump($_POST)显示:

{ ["hours"]=> string(1) "1" ["member3"]=> string(8) "5101813/" }

But in your ajax request, there's no such member3 , and memberID isn't present in $_POST. 但是在您的ajax请求中,没有此类member3 ,并且$ _POST中不存在memberID As other have mentioned, maybe the value for memberID is null and not being sent - but that doesn't explain where member3 comes from. 正如其他人提到的那样,memberID的值可能为null且未发送-但这不能解释member3来源。

I'd suggest figuring out why you have member3 in there, where it's coming from, and doing debug on the memberID value on the javascript side to find out if it's null. 我建议弄清楚为什么那里有member3 ,它来自哪里,并在javascript端对memberID值进行调试,以找出它是否为null。

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

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