eevaa
eevaa

Reputation: 1457

jQuery AJAX post, redirect and THEN prepend

I am posting data to a php file, which returns a message. Then I'd like to redirect to a new page and display the message at the top of the new page.

I was thinking of something like this, except that I want to prepend AFTER redirecting.

$.post('process.php', {"data": data}, function( message ){
    window.location.pathname = '/';
    $( '#content' ).prepend( message );
});

Upvotes: 0

Views: 72

Answers (1)

Rahil Wazir
Rahil Wazir

Reputation: 10142

You can use JavaScript localStorage API to save message and display to the page.

$.post('process.php', {"data": data}, function( message ){
    localStorage.setItem('message', message); //Store the message
    window.location.pathname = '/';
});

Then on next page where redirection made:

<script>
$(document).ready(function() {
   $('#content').prepend(localStorage.getItem('message')); //Display the content if null, nothing will be insert to DOM
});
</script>

Upvotes: 1

Related Questions