How to use javascript to go to previous page after running other code?

228 Views Asked by At

I have a reporting website that I maintain. I have it setup so that users can select a row and be taken to a form to edit parts of it. After making the edits the user will click the "Submit" button and then a query is run against the SQL DB to update the record in the DB. I want the page to then go back to the referring page. I know that there's a way to do that with javascript something like parent.history.go(-1);, but I don't know how to implement it.

Basically, after the $sth->execute(); I want something that will send the parent.history.go(-1);.

I was trying to use header('Location:') but that would only work if they had not done a search on the previous page (or any filtering or ordering). If I could have it just go back to the previous page, or maybe even reload the previous page so that their edits show up too that would be great.

2

There are 2 best solutions below

4
On BEST ANSWER

You can use header('Location: '.$url) along with state saving in jQuery DataTables (since your question is tagged datatables-1.10) with stateSave option.

For example:

$('#example').dataTable( {
   stateSave: true
});

It will save/restore table sorting, filtering and page so that when the same page is visited again the table will have previous state restored.

3
On

You can use the back method.

function goBack() {
    window.history.back();
}