pawel
pawel

Reputation: 6136

Getting response from PHP in jquery $.post or equivalent

I'm sending JQUERY $.post request to my php method, it looks like that:

$.post("/controller/method/",{id: cell_id},function(data,status,xhr)
{
         some_javascript_function();
})

PHP:

public function method()
{
   insert to database code...
}

my PHP function just insert a record into table, which some_javascript_function() will use - cause it request data from database. Sometimes my PHP function is too slow to insert record before some_js_function() tries to get it from database.

Hoe can I manage some response from PHP when everything is ready, and then call some_js_function?

Upvotes: 0

Views: 101

Answers (2)

Techie
Techie

Reputation: 45124

What you have to do is return data from the PHP function after the insertion is done. It would look like below.

function store()
{
    //your code......

    //let's assume that store function will do the data storing part.

    $state = store(); // you should return true after the data insertion is successful

    if($state)
     echo $data; // here we echo data to used in the callback function.
}

Callback function is executed after the current effect is 100% finished.

Upvotes: 1

Halcyon
Halcyon

Reputation: 57709

Start the read after the write has returned. $.post has a callback function, that's when you're guaranteed the write has completed.

Upvotes: 1

Related Questions