最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Refresh a div with jQuery - Stack Overflow

matteradmin2PV0评论

Is it possible to refresh a single div with jQuery? I have a button that does logout a user. After the button is clicked, I want to show the login form, so I need to re-parse this div.

The content of my div is this:

<div id="signup" class="register">

    <?php
        $email = $_COOKIE['email'];
        $login = $_COOKIE['login'];

        if($email != null && $email != "" && $login != null && $login != "") {
            echo "<h3>you are logged in as <b>$email</b></h3><br><br><button id='logoutbtn' class='submitButton'>Logout</button>";
        } else {
            // show login form
        }
    ?>

</div>

This is my JavaScript part where I perform the logout:

$('#logoutbtn').click(function () {    
    $.ajax({  
        type: "POST",  
        url: "php/logout.php",  
        async: false,
        success: function(data){
            // reload signup div
        } 
    });  
});

Edit: My logout.php script removes the cookies, that's why I need to re-parse the signup-div.

Is it possible to refresh a single div with jQuery? I have a button that does logout a user. After the button is clicked, I want to show the login form, so I need to re-parse this div.

The content of my div is this:

<div id="signup" class="register">

    <?php
        $email = $_COOKIE['email'];
        $login = $_COOKIE['login'];

        if($email != null && $email != "" && $login != null && $login != "") {
            echo "<h3>you are logged in as <b>$email</b></h3><br><br><button id='logoutbtn' class='submitButton'>Logout</button>";
        } else {
            // show login form
        }
    ?>

</div>

This is my JavaScript part where I perform the logout:

$('#logoutbtn').click(function () {    
    $.ajax({  
        type: "POST",  
        url: "php/logout.php",  
        async: false,
        success: function(data){
            // reload signup div
        } 
    });  
});

Edit: My logout.php script removes the cookies, that's why I need to re-parse the signup-div.

Share Improve this question asked Jun 1, 2013 at 16:14 nimrodnimrod 5,74230 gold badges89 silver badges152 bronze badges 1
  • You can just use something like $("#signup").html(data) in your success function to overwrite the Div’s content with your PHP script’s output … – C3roe Commented Jun 1, 2013 at 16:17
Add a ment  | 

4 Answers 4

Reset to default 3

You can dynamically change the contents of the signup div with jquery as follows:

$("#signup").html("My New Login Section");

or

var $loginDiv = $(document.createElement("div")).html("<label>Username</label><input type='text' id='username'/>");
$("#signup").html($loginDiv);

Use load()

$('#signup').load('php/logout.php', function() {
  alert('Logout.');
});

I'd try:

$('#logoutbtn').on('click', function () {    
    $.ajax({  
        type: "POST",  
        url: "php/logout.php"
    })
    .done(function(data){
        $('#signup > h3').html('Please <button id="loginbtn" class="submitButton">Login</button>');
    });
})

The same approach could be used for the login action to prevent the page from having to refresh.

as you have your session method inside of your tag, you can do on success event following:

success: function(data){
        $("#signup").load(".register");
    } 

it will reload everything inside of < div id="signup" class="register"> and as you've removed your session (i suppose), than it will be reload to your login/logout form.

Post a comment

comment list (0)

  1. No comments so far