简体   繁体   中英

AJAX load page and reload/add after success

So I have a page which, when requested, updates my database. For example when I go to database_update.php it just updates the database and doesn't show anything.

Index.php shows user database content.

So I have function in JavaScript called update which must use AJAX to run this page and after the page loads (after all queries run successfully) it must load index.php and show updated page to the user (reload the page without refresh effect).

My code is like:

$.get('ajax_update_table.php', {
    // The following is important because page saves to another table
    // users nick which call update:
    update: UserLogin
}, function (output) {
    // The following is unimportant:
    $(myDiv).html(output).show();
});

Two suggestions:

  1. Return a "success" or "error" code from your database_update script. Returning a JSON string is very easy. For example:

     echo '{"success":"success"}'; 
  2. Use the $.ajax function. Then add the success, error, and complete parameters. You can call any javascript function(s) when the AJAX request is complete.

     $.ajax({ url: 'update_database.php', dataType: 'json', success: function (data) {successFunction(data)}, error: function () { error(); }, complete: function () { complete(); } }); function successFunction(data) { if ('success' in data) { // Do success stuff here. } else { // Show errors here } } // .... etc 

I might be missing something, but I'll try to answer your question.

I would setup a blank page that on loading it sends the index.php to a div on that page. For example, make a page titled blank.php.

blank.php would have the following:

function Index(){
$.ajax({
    url:"index.php",
    type: "GET",
    success:function(result){
        $("#web-content").html(result);
    }
});
}

<script type="text/javascript">Index();</script>
<div id="web-content"></div>

You would then have your index execute the Index function to update the web-content div with the index.php data without reloading the blank.php page.

Got it to work!

my index.php

<html>
<head>
    <script type="text/javascript" language="Javascript" SRC="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" language="Javascript">
    function update_and_replace()
        {
            $.ajax({
                url: 'ajax_update_table.php',
                dataType: 'json',
                success: function (data) {successFunction(data)},
                error: function () { error(); },
                complete: function () { complete(); }
            });
        }

        function successFunction(data) {
            if ('success' in data) {
                $("#content").load("index.php");
            } 
        }

    </script>
</head>
<body>
<div id='content'>
Now is: <?php echo date("Y-m-d, H:i:s");?>
<br/><br/><br/><br/>
<a href="javascript:void(0);" onClick="update_and_replace();">Aktualizuj</a>
</div>
</body>
</html>

Ajax_update_table.php

<?php echo '{"success":"success"}'; ?>

Thank You all for your help. I know that my English is bad but with your help I made it!

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