简体   繁体   中英

Refresh browser with PHP function after Javascript & AJAX POST

My Javascript allows user to logon to 3rd party system, returning an array of user details. This works perfectly :)

I use JSON and AJAX to POST it to a PHP script. This also works.

The PHP script interrogates my mySQL database, returning various details. This works too!

However, I can only view the mySQL output in Safari's Web Inspector. I presume I need to get the PHP script to "refresh" the browser to display the output, but cannot find how to do.

Help appreciated.

You'll need to do this with JavaScript.

When your ajax returns, you'll want to load that into your page. In your success handler update a DOM element to contain the user details you're interested in.

Using jQuery: (and very rudimentary)

HTML:

...
<div id="userLoginDetails"></div>
...

JS:

$.post('ajax/login.php', { username: 'john doe', password: 'blah' }, function(data) {
  $('#userLoginDetails').html(data);
});

You can use this code to push the control from PHP:

<?php
    header('Content-type: text/javascript');
    die('location.href=location.href;');
?>

Or even by JavaScript, after the AJAX load, in the success function you can include this:

success: function() {
    // Other Codes
    location.href=location.href
}

The penny dropped. All I needed was the AJAX "success" function - Duh. Thanks for all your help.

    $.ajax({        
    type: "POST",
    url: "AddEdit_Interests.php",
    data: { interestsArray : result },
    success: function(data) {
         $("#lengthQuestion").fadeOut('slow'); 
         document.write(data);
    }
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM