I'm using ajax/json to send values to a php function that would then display the data on the page. I'm sending two values $hours
and $memberID
, except I seem to only be able to retrieve $hours
and not $memberID
. I ran the page through Firebug, on the javascript page the values for both variables are being read right before they're sent. In the PHP form, I ran multiple conditions/echo statements to print out both values except only $hours
is being displayed. Any ideas?
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:
//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):
echo "<input type='checkbox' name='member{$attNumber}' id='member{$attNumber}' value=$studentID/>$attendees<br />";
Edit: If I run a var_dump($_POST['memberID']);
, it prints out NULL
. If I run a var_dump($_POST)
, it prints out array(2) { ["hours"]=> string(1) "1" ["member3"]=> string(8) "5101813/" }
.
Edit: I added in the PHP code that uses member3
. Edit 2: That's the entire Javascript function. The PHP code is all that I have for processing the data.
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. 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.
memberID.value // check this value
Can you check that memberID.value is returning the expected 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. 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. 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:
$.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
However, you state that var_dump($_POST)
shows:
{ ["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. As other have mentioned, maybe the value for memberID is null and not being sent - but that doesn't explain where member3
comes from.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.