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();
Chad Priddle
  • 650
  • 2
  • 15
  • 32
  • 2
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Mar 13 '15 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. – A. Duff Mar 13 '15 at 18:51
  • even if I comment out the redirect I still can't get the db to update – Chad Priddle Mar 13 '15 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? – Quentin Mar 13 '15 at 18:59
  • yes the update script works – Chad Priddle Mar 13 '15 at 19:03

3 Answers3

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 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. – Roger Halliburton Mar 13 '15 at 19:08
  • if I alert the data of each variable I get the correct variables – Chad Priddle Mar 13 '15 at 19:17
  • 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. – skidadon Mar 13 '15 at 19:30
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.

ecarrizo
  • 2,758
  • 17
  • 29
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.

skidadon
  • 547
  • 4
  • 7