Reputation: 672
I've searched and cannot find an answer to this. I have an ajax that loops and waits for a variable and when it does not equal 0 I want to send the data to another script so that it can update the db and then it redirects to another URL. I've tried .post and .get and cannot get the values to update the db but the script does redirect.
function updateTrigger(){
$.ajax({
url: "json.trigger.php",
dataType: "json",
cache: false,
success: function(data) {
var scrape_type = data.scrape_type;
var account_id = data.account_id;
if(account_id != 0) {
$.post('update-pending.php', {account_id: account_id, scrape_type: scrape_type});
document.location.href="redirect.com";
}
}
});
}
Here is my update-pending.php that I am sending the variables to first before it redirects.
$account_id = $_POST['account_id'];
$scrape_type = $_POST['scrape_type'];
$stmt = $con->prepare("UPDATE triggers SET
pending='1'
WHERE account_id = '$account_id' AND scrape_type = '$scrape_type'") or die(mysql_error());
$stmt->execute();
Upvotes: 0
Views: 354
Reputation: 2809
IMPORTANT: Check the last part of the answer, you NEED to change your PHP code in order to avoid SQL Injection.
You are generating the corresponding promise for the POST request, but it never is executed because you are leaving the page before that happen.
Simple call .done method with a callback as a parameter from your $.post() promise, that callback it's going to be executed after the post succeed, also yo can consider add a .fail() that it's going to be executed if the post fail.
$.post( 'update-pending.php', {
account_id: account_id,
scrape_type: scrape_type
}).done(function() {
document.location.href="redirect.com";
}).fail(function() {
//enter code here
};
In the server you need return a response to the post request at the end of the process. If not you $.post will return error and execute .fail() always.
If you can't / don't want to wait the php script finalize its execution to redirect the user to redirect.com consider to execute a secondary script in the background in the server.
I updated your pending.php to use PDO:
//update-pending.php
/* Consider executing this in the background, (adapted) */
$account_id = $_POST['account_id'];
$scrape_type = $_POST['scrape_type'];
$params = array('account_id' => $account_id, 'scrape_type' => $scrape_type);
$query = "UPDATE triggers SET pending='1'
WHERE account_id = :account_id
AND scrape_type = :scrape_type";
$stmt = $con->prepare($query);
$stmt->execute($params);
/* End of background script */
return true; // Return a response to the POST request.
No tested (Can't now) but you get the idea.
Upvotes: 1
Reputation: 547
1) The easiest way to address this is to make use of the built in javascript function settimeout(). But dealing with set timers is scary because you don't know when the script is going to complete.
2) The not-so easiest way to address this is to implement a callback function that gets triggered after the first method completes. Actually, it's not that hard to build.
3) The other way may be implementing some form of "jsonp" implementation for working with and interacting with your php script.
If it's a quick hack, just go with option 1. If not, play around with a combination of 2 and 3. Good luck partner.
Upvotes: 0
Reputation: 944537
You are leaving the page immediately after running the JS that triggers the POST request. This causes the POST request to be cancelled.
You need to wait until you get a response to the POST request before setting location
to a new value.
Upvotes: 1