0

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();
5
  • 2
    Danger: You are vulnerable to SQL injection attacks that you need to defend yourself from. Commented Mar 13, 2015 at 18:48
  • I would try setting the document.location.href = assignment to a callback to the $.post function. Perhaps the redirect is being processed before the $.post can do anything. Commented Mar 13, 2015 at 18:51
  • even if I comment out the redirect I still can't get the db to update Commented Mar 13, 2015 at 18:55
  • Have you checked your error log / response to see if the statement is being prepared correctly? Have you checked the return value of execute() to see if it is being executed correctly? Commented Mar 13, 2015 at 18:59
  • yes the update script works Commented Mar 13, 2015 at 19:03

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I would want to test this, but I'm pretty sure the POST would actually initiate before the page location changes, the problem is that the event handler for the post response would be wiped out so you would never see what it says. I suspect the actual error lies somewhere in the initial ajax call, which is perhaps generating bad json data for the POST.
if I alert the data of each variable I get the correct variables
The A in Ajax stands for asynchronous. Have to agree with Quentin here. And Chad, 'alert' or 'confirm' or 'prompt' causes the page to wait until you close the modal. So that's why it works for alert. Otherwise it's asynchronous.
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.