user2361114 user2361114 - 1 month ago 10
Ajax Question

Calling another ajax function on ajax success

I'm calling an ajax function for user login.
After successful login, I want to refresh a div with content like (Ex. Welcome Mr. User, logout).

HTML:

<ul id="signup">
<li>Login</li>
<li>Signup</li>
</ul>

<ul id="loginbox">
<li><label>Enter email address</label><br/><input type="text" class="bookinginput" id="uemail"></li>
<li><label>Enter password</label><br/><input type="password" class="bookinginput" style="width: 151px;" id="upassword">
<button class="styled-button-8" style="margin-top: -43px;margin-left: 10px;" onClick="ulogin()">Login</button>
</li>
</ul>


Ajax:

function ulogin()
{
var uemail = $('#uemail').val();
var upassword = $('#upassword').val();
$.ajax({
type: "POST",
url: "udoologin.php",
data: {email:uemail, password:upassword}
}).done(function( result ) {
$("#loginbox").html(result);
});
}


After successful login, I'm able to change div#loginbox with successfull login message, but I want to replace another div#signup also with

<ul id="signup">
<li>Welcome <?php echo $user?></li>
<li>logout</li>
</ul>


How can I achieve this?

Answer Source

In PHP when u are printing the result,echo

<li>Welcome <?php echo $user?></li>
   <li>logout</li>

joined with a seperator like '|||' or something.

in jquery

done(function( result ) {
 var log=result.split('|||');
 $("#loginbox").html(log[0]);
 $('#signup').html(log[1]);

});