Reputation: 2685
I have the following ajax code sending data to php via post:
checked_id = 10;
$.ajax({
url: "/delete_document",
type: "POST",
data: {id:checked_id}, // a
success: function(data, textStatus, jqXHR){
alert(data);
location.href='/delete_document';
},
error: function (jqXHR, textStatus, errorThrown){
//alert('Error!')
}
});
it prepares the data successfully, and the correct value is alerted (10).
Then i should fetch this value in the php file:
$selected_id = $_POST['id'];
var_dump($selected_id);
but var_dump gives me string(0) "".
I know that when the code doesn't work, it's always me the one that's doing it wrong. But I can really not see the mistake here.
Upvotes: 0
Views: 52
Reputation: 15656
The thing is that what you eventually see after this code has completed is not a result of POST ajax call, but it is the result of redirection in line
location.href='/delete_document';
And this makes a second call to /delete_document
URL which is this time simple GET request without the POST data that you expect it to have.
Edit.
Instead of making an ajax call and redirecting to that page you want to move to /delete_document
with POST data. You can't do that with redirecting so you should make a form and submit it in JavaScript.
Here's an example of what you're trying to do: JavaScript post request like a form submit
Upvotes: 2