I am submitting a POST request via AJAX to my PHP script. The browser tools Network
tab shows the POST data has values, on arrival at my PHP script the $_POST
value is true, isset()
verification is valid, and there should be no problem to assign the $_POST['input']
values to my $variables
.
Yet, my debug bar tells me that the PHP variables like $first_name
are infact empty. I am feeling very stupid here, what am I missing?
AJAX
$('#newUserSubmit').click(function() {
console.log('Submit button clicked.');
if ($('#addNewUser').valid()) {
console.log('Form on submit is valid');
$.ajax({
type: 'POST',
url: '../../controllers/admin_addNewUser.php',
data: {
action: 'add_new_user',
user_data: $('#addNewUser').serialize()
},
cache: false,
success: function(data) {
alert(data);
console.log('Ajax POST request successful.');
},
error: function(xhr, status, error) {
console.log('Ajax POST request failed.');
console.error(xhr);
}
});
} else {
console.log('Form on submit is invalid');
return false;
}
});
Browser Network Tab:
Request Data
MIME Type: application/x-www-form-urlencoded; charset=UTF-8
action: add_new_user
user_data: first_name=John&last_name=Doe
PHP
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$debug_msg = 'POST check: passed<br>';
if (isset($_POST['action']) && ($_POST['action'] == 'add_new_user')) {
$debug_msg .= 'ISSET check: passed<br>';
// sanitize
$fn = mysqli_real_escape_string($db, $_POST['first_name']);
$ln = mysqli_real_escape_string($db, $_POST['last_name']);
}
}
Response:
<b>Notice</b>: Undefined index: first_name in <b>/path/to/script/admin_addNewUser.php</b> on line <b>27</b><br />
<br />
<b>Notice</b>: Undefined index: last_name in <b>/path/to/script/admin_addNewUser.php</b> on line <b>28</b><br />
Appreciate any help on this, been staring at this for too long already.
Thanks
My Answer:
In your AJAX request, you are serializing the form data using $('#addNewUser').serialize(), which creates a URL-encoded string of form data. This means that the data sent to the server will be in the format of key-value pairs, like "first_name=John&last_name=Doe".
However, in your PHP script, you are trying to access $_POST['first_name'] and $_POST['last_name'], which do not exist in the serialized data. Instead, you should access the values using the keys 'first_name' and 'last_name' in the 'user_data' array.
Here's how you can modify your PHP script to access the values correctly:
php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$debug_msg = 'POST check: passed
';
if (isset($_POST['action']) && ($_POST['action'] == 'add_new_user')) {
$debug_msg .= 'ISSET check: passed
';
parse_str($_POST['user_data'], $userData);
// sanitize
$fn = mysqli_real_escape_string($db, $userData['first_name']);
$ln = mysqli_real_escape_string($db, $userData['last_name']);
}
}
By using parse_str($_POST['user_data'], $userData), you can convert the URL-encoded string into an associative array, allowing you to access the values using the correct keys.
This should solve the issue of empty POST values in your PHP script.
Rate this post
5 of 5 based on 7313 votesComments