Reputation: 482
I am trying to send some data with post to a php file but for some reason its not being posted. I receive an empty array in console log from the php file on var_dump
.
Here is code I have in these files:
index.php
<button type="button" class="btn btn-warning" id="resetPassword">Reset Password</button>
<script>
$(document).ready(function (e){
$("#resetPassword").click(function(e){
e.preventDefault();
$.ajax({
url: "scripts/process.php",
type: "POST",
data: {name: 'test'},
cache: false,
processData:false,
success: function(data) {
if (data === 'Success') {
new PNotify({
title: 'Success!!!',
text: 'Request completed successfully.',
type: 'success',
styling: 'bootstrap3'
});
$('#exampleModal').modal('hide');
$('#datatable-buttons').DataTable().ajax.reload();
} else {
console.log(data);
new PNotify({
title: 'Error!!!',
text: data,
type: 'error',
styling: 'bootstrap3'
});
//document.getElementById("status").className += " alert-danger";
//$("#status").html(data);
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert("ERROR:" + xhr.responseText+" - "+thrownError);
}
});
});
});
</script>
Here is the php file process.php.
var_dump($_POST);
Result in console.log:
array(0) {}
Solution:
Remove processData: false
thanks guys.
Upvotes: 2
Views: 723
Reputation: 4584
If you try by processData:false
,its need data is raw string
change from
data: {name: 'test'},
into
data: "name=test",
Upvotes: 0
Reputation: 2177
What you are using while returning data in process.php file? ECHO or RETURN?
Try ECHO, if it does not work then try RETURN statement. Rest of your code seems OK.
To debug use print_r() on scripts/process.php and check it in browser's console for out put. Also, you can use toSource of javascript to check variable's data
Upvotes: 0
Reputation: 1574
Try this before or after
e.preventDefault();
var formData = $("#yourform_id").serializeArray(); //form data
and replace data with this line datas
data: formData,
remove
processData:false,
Upvotes: 0