| | |
Problem passing email address through AJAX
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
I am facing problems passing an email address to a php file through AJAX. The code I am using is pretty basic but I can't seem to get it to work. Could anyone help me understand what's going wrong? I amd pretty new to Ajax (about 2 days old) so please forgive me if this seems pretty lame.
The PHP file is also pretty basic...
Thanks in advance...
javascript Syntax (Toggle Plain Text)
function emailchk () { if (!document.getElementById("email").value) { document.getElementById("emailmsg").innerHTML = " "; } else { url = "emailchk.php?email=" + document.getElementById("email").value; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else { if (window.ActiveXObject) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } } if (xhr) { xhr.onreadystatechange = showEmailContents; xhr.open("GET", url, true); xhr.send(null); } else { alert("Sorry, but I couldn't create an XMLHttpRequest"); } return false; } } function showEmailContents() { if (xhr.readyState == 4) { if (xhr.status == 200) { var outMsg = xhr.responseText; } else { var outMsg = "There was a problem with the request " + xhr.status; } document.getElementById("emailmsg").innerHTML = outMsg; if (outMsg == "Email already exists") { document.getElementById("validate").value = "no"; } else { document.getElementById("validate").value = "yes"; } } }
The PHP file is also pretty basic...
php Syntax (Toggle Plain Text)
<?php include ("dbconfig.php"); $sql = "select name from users where email = '$_POST[email]'"; $result = mysql_query ($sql, $conn) or die ("could not query database"); if (mysql_num_rows($result)) { print "Email already exists"; } else { print "Email is available"; } ?>
Thanks in advance...
Last edited by cscgal; Sep 19th, 2008 at 7:55 pm. Reason: spelling mistakes / Fixed code tags
change drives evolution - without change we'd probably still be up in the trees
Try to recall all the lines of your code!
Am not sure if this wil work, but it never hurts to try!
Good day to you!
javascript Syntax (Toggle Plain Text)
function emailchk () { //Refering to what value?--> if (!document.getElementById("email").value) { document.getElementById("emailmsg").innerHTML = " "; } else { url = "emailchk.php?email=" + document.getElementById("email").value; //Missing curly!--> } //So we'l have to add our end curly for this block! if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } /* else { * This is not necessary for this line! */ if (window.ActiveXObject) { try { xhr = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } } if (xhr) { xhr.onreadystatechange = showEmailContents; xhr.open("GET", url, true); xhr.send(null); } else { alert("Sorry, but I couldn't create an XMLHttpRequest"); } return false; } function showEmailContents() { if (xhr.readyState == 4&& xhr.status == 200) { var outMsg = xhr.responseText; } else { var outMsg = "There was a problem with the request " + xhr.status; } document.getElementById("emailmsg").innerHTML = outMsg; if (outMsg == "Email already exists") { document.getElementById ("validate").value = "no"; } else { document.getElementById ("validate").value = "yes"; } }
Am not sure if this wil work, but it never hurts to try!
Good day to you!
Actually the code is working but it doesn't seem to pass the email address to the php file. If I try to pass a normal string that seems to work - but for some reason, I cannot pass an email address as a GET variable. Can it be something to do with the @ sign?
change drives evolution - without change we'd probably still be up in the trees
> Can it be something to do with the @ sign?
Yes.
Make sure you *always* encode user entered data; at least when making asynchronous calls using Ajax. Normal submits automatically encode the form data for you. Use encodeURIComponent.
Yes.
Make sure you *always* encode user entered data; at least when making asynchronous calls using Ajax. Normal submits automatically encode the form data for you. Use encodeURIComponent.
I don't accept change; I don't deserve to live.
![]() |
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Jumping to specific point in real player
- Next Thread: Javascript Focus() with tabs?
| Thread Tools | Search this Thread |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate array automatically browser bug calendar captchaformproblem cart checkbox child close codes createrange() cursor date debugger dependent disablefirebug dom dropdown editor element embed engine enter events explorer ext file firefox form forms frameworks getselection google gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump libcurl maps masterpage math media microsoft object onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post programming progressbar prototype redirect regex runtime safari scale scriptlets scroll search security shopping size software sql text textarea unicode web website windowsxp wysiwyg \n






