First off: I do not want to use JQuery, i know it would make it easier but it would ravish the porpuse of the thing.
Note: ajaxFunction is the default ajax exemple for a GET/POST
Now, after clicking the function, the first time, it does work and everyrhing gets replaced by the php-echo. The problem is AFTER.
After changing the initia-div (“login-place”) to the next div (“regit”) the javascript function of sregit.onClick = function() does not work.
How do i go around this? Should i create the DOMElement in PHP and use xmlSave(“index.php”)?
initial-div:
<div id="login-place">
<table id="login-init" name="login-init">
<tr>
<td>username</td>
<td><input type="text" name="user" id="user" /></td>
<td>password</td>
<td><input type="password" name="pass" id="pass" /></td>
</tr>
<tr>
<td colspan="2"><input name="login" id="login" type="button" value="entrar" /></td>
</tr>
</table>
</div>
the JS code:
var slogin = document.getElementById("login");
slogin.onclick = function() {
//alert("inside."+user.value);
s = "goforth=1&user="+user.value+"&pass="+pass.value;
ajaxFunction('4',""+s);
}
var sregit = document.getElementById("regit");
sregit.onclick = function () {
alert("inside."+user.value);
s = "regit=1&user="+user.value+"&pass="+pass.value;
ajaxfunction('4',""+s);
}
function ajaxFunction(arg,string) {
var getdate = new Date(); //Used to prevent caching during ajax call
if(xmlhttp) {
if (arg==4) {
nextland = "login-place";
document.getElementById("all-burps").innerHTML=string;
xmlhttp.open("POST","session.php",true);
}
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
if (arg==4) {
xmlhttp.setRequestHeader('Content-length', string.length);
xmlhttp.send(""+string);
}
}
}
function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
if (nextland != "login-place") { document.getElementById(nextland).innerHTML=xmlhttp.responseText; } //Update the HTML Form element
else {
// alert(xmlhttp.responseText); -> this is true no error up here.
var thediv = document.getElementById("login-init");
thediv.parentNode.removeChild(thediv); //this happens fine.
var theplace = document.getElementById("login-place");
var thelement = document.createElement("div"); //this too.
thelement.innerHTML=xmlhttp.responseText; //this too'
theplace.appendChild(thelement); //this too.
}
}
And, the innerhtml.responseText is =
echo '
<table id="login-2" name="login-2">
<h3>o username ' . $username . ' não existe. <a href="#" id="register-link" name="register-link">Queres registar?</a></h3>
<tr>
<td>username</td>
<td><input type="text" name="user" id="user" value="' . $username . '"/></td>
<td>password</td>
<td><input type="password" name="pass" id="pass" value="' . $password . '" /></td>
</tr>
<tr>
<td colspan="2"><input type="button" value="entrar" id="regit" name="regit"/></td>
</tr>
</table>
';
I hope this is enough for anyone to understand my doubt. else i’ll reword it.
The call in the function that does not work says
ajaxfunction, whereas the function is calledajaxFunction– Javascript is case sensitive.Also, you need to move the declaration of the
onclickfunction forregitto inside thehandleServerResponse()function, after the linethelement.innerHTML=xmlhttp.responseText;, because before this line is called, there is no element withid="regit", so the binding of the function won’t work.Also, you should terminate the functions declared as
element.onclick = function () {}with a;. Declaring functions in this manner is an assignment statement and as such it requires a semi-colon to terminate it.EDIT here is your code reworked so hopefully it does what you want, with some extra bloat removed. I am assuming that
xmlhttp,nextland,userandpasshave been declared in some code you haven’t posted – have they?