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
Reply

Join Date: Mar 2007
Posts: 5
Reputation: positrix is an unknown quantity at this point 
Solved Threads: 0
positrix's Avatar
positrix positrix is offline Offline
Newbie Poster

Problem passing email address through AJAX

 
0
  #1
Sep 19th, 2008
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.

  1. function emailchk () {
  2. if (!document.getElementById("email").value) {
  3. document.getElementById("emailmsg").innerHTML = " ";
  4. }
  5. else {
  6. url = "emailchk.php?email=" + document.getElementById("email").value;
  7. if (window.XMLHttpRequest) {
  8. xhr = new XMLHttpRequest();
  9. }
  10. else {
  11. if (window.ActiveXObject) {
  12. try {
  13. xhr = new ActiveXObject("Microsoft.XMLHTTP");
  14. }
  15. catch (e) { }
  16. }
  17. }
  18.  
  19. if (xhr) {
  20. xhr.onreadystatechange = showEmailContents;
  21. xhr.open("GET", url, true);
  22. xhr.send(null);
  23. }
  24. else {
  25. alert("Sorry, but I couldn't create an XMLHttpRequest");
  26. }
  27. return false;
  28. }
  29. }
  30.  
  31. function showEmailContents() {
  32. if (xhr.readyState == 4) {
  33. if (xhr.status == 200) {
  34. var outMsg = xhr.responseText;
  35. }
  36. else {
  37. var outMsg = "There was a problem with the request " + xhr.status;
  38. }
  39. document.getElementById("emailmsg").innerHTML = outMsg;
  40. if (outMsg == "Email already exists") {
  41. document.getElementById("validate").value = "no";
  42. }
  43. else {
  44. document.getElementById("validate").value = "yes";
  45. }
  46. }
  47. }

The PHP file is also pretty basic...

  1. <?php
  2. include ("dbconfig.php");
  3. $sql = "select name from users where email = '$_POST[email]'";
  4. $result = mysql_query ($sql, $conn) or die ("could not query database");
  5.  
  6. if (mysql_num_rows($result)) {
  7. print "Email already exists";
  8. }
  9. else {
  10. print "Email is available";
  11. }
  12.  
  13. ?>

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Problem passing email address through AJAX

 
0
  #2
Sep 19th, 2008
Try to recall all the lines of your code!

  1. function emailchk ()
  2. { //Refering to what value?-->
  3. if (!document.getElementById("email").value) {
  4. document.getElementById("emailmsg").innerHTML = " ";
  5. }
  6. else {
  7. url = "emailchk.php?email=" + document.getElementById("email").value; //Missing curly!-->
  8.  
  9. } //So we'l have to add our end curly for this block!
  10.  
  11. if (window.XMLHttpRequest) {
  12. xhr = new XMLHttpRequest();
  13. }
  14.  
  15. /* else { * This is not necessary for this line! */
  16.  
  17. if (window.ActiveXObject) {
  18. try {
  19. xhr = new ActiveXObject("Microsoft.XMLHTTP");
  20. }
  21. catch (e) { }
  22. }
  23. if (xhr) {
  24. xhr.onreadystatechange = showEmailContents;
  25. xhr.open("GET", url, true);
  26. xhr.send(null);
  27. }
  28. else {
  29. alert("Sorry, but I couldn't create an XMLHttpRequest");
  30. }
  31. return false;
  32. }
  33.  
  34. function showEmailContents() {
  35. if (xhr.readyState == 4&& xhr.status == 200) {
  36. var outMsg = xhr.responseText;
  37. }
  38. else {
  39. var outMsg = "There was a problem with the request " + xhr.status;
  40. }
  41. document.getElementById("emailmsg").innerHTML = outMsg;
  42.  
  43. if (outMsg == "Email already exists") {
  44. document.getElementById ("validate").value = "no";
  45. }
  46. else {
  47. document.getElementById ("validate").value = "yes";
  48. }
  49. }

Am not sure if this wil work, but it never hurts to try!
Good day to you!
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 5
Reputation: positrix is an unknown quantity at this point 
Solved Threads: 0
positrix's Avatar
positrix positrix is offline Offline
Newbie Poster

Re: Problem passing email address through AJAX

 
0
  #3
Sep 20th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 9
Reputation: poddarpallavi22 is an unknown quantity at this point 
Solved Threads: 0
poddarpallavi22 poddarpallavi22 is offline Offline
Newbie Poster

Re: Problem passing email address through AJAX

 
0
  #4
Oct 14th, 2008
If you are able to pass other strings except email them try to url encode the email to send it
u can do the following
url = "emailchk.php?email=" +URLencode(document.getElementById("email").value);

It should work fine.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,608
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 463
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Problem passing email address through AJAX

 
0
  #5
Oct 14th, 2008
> 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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC