...

View Full Version : Resolved cant pass values to Ajax function..



cyborg911
04-11-2011, 03:57 PM
I am using Ajax with PHP to authenticate id and pass at my site.
while the php script is working fine, the ajax is giving problems..
The http://127.0.0.1/abhishek.html? comes to http://127.0.0.1/abhishek.html?username=hello&pass=world where hello is the id and world is the pass instead of loading the php script "idpass.php". I guess the values from the textbox are not being passed to Ajax function..




function showUser(str,str1)
{
if (str=='')
{
document.getElementById("demo").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("demo").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","idpass.php?q="+str +"&r="+str1,true);
xmlhttp.send();
}

<form onSubmit=showUser(this.value,this.value) >
<div id="txt"><b>Enter your username </b>
<input type="text" name= "username">
<br>
<b>Enter your password</b> <input type="password" name="pass">
<br></div>
<div id="txt1" style="display : none">help</div>
<input type="submit" value="Login" />
</form>

devnull69
04-11-2011, 04:25 PM
Ok, I think you should consider a few things here

1) Put all attribute values into double quotes
2) on-handlers should be written in lower case
3) A form does not have a value ... so this.value in onsubmit is pointless
4) You should return false from your onsubmit handler, otherwise the page will be submitted and refreshed afterwards

Something like that:


<form onsubmit="showUser(); return false;">


In showUser() you can access the fields much easier if you give them id attributes like


<input type="text" name= "username" id="user">


Then you can get the value using


document.getElementById('user').value



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum