Is there any way to make a function immediately after its execution?

I have a button SIGN INthat onclickcalls a function login(). Inside, login()I call 2 functions:

function login()
{
    signin();
    statistics_table();
    return false;
}

signin();performs the actual signing ( $_SESSION['name']=$username;) ( ajax), and statistics_table();prints a table associated with the user, or a text indicating "please log in to see your statistics" (if the signature did not work) ( ajax).

The problem statistics_table()always prints "please log in ..." . It is as if he signin()had not yet completed the actual signing. Apparently, it only executes it after the function returns login().

So, is there any way to do signin()apply my actions immediately after its completion?

I also tried to make sure that the button calls a function signin(), and statistics_table()one by one, for example:

<input type="submit" value="Sign In" onclick="signin(); statistics_table();">

but that way, when I click the button, it causes the page to reload (again, I need it to be ajax).

Thanks in advance

change

Here is the implementation signin():

function signin()
{
    var name = document.getElementById('name').value;
    var pw= document.getElementById('password').value;
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("Test").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("POST","members.php?name="+name+ "&password="+pw,true);
    xmlhttp.send(); 
    return false;
}
+3
source share
1 answer

You just need to move the call statistics_table()inside the callback onreadystatechangefor readyState == 4, so it gets called after the user login successfully completes.

xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        // You're already writing some output with the XHR response
        document.getElementById("Test").innerHTML=xmlhttp.responseText;

        // So no problem writing a little more in the user table:
        statistics_table();
    }
}

And the caller login()then boils down to:

function login()
{
    signin();
    return false;
}

: , POST, , PHP $_GET. .send():

xmlhttp.open("POST","members.php", true);
// Set the correct Content-type header for a POST form
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("name=" + encodeURIComponent(name) + "&password="+encodeURIComponent(pw));
+1

All Articles