stijn0713 Posted April 14, 2012 Share Posted April 14, 2012 I wrote a code to check the uniqueness of an email and wheter it's the correct pattern. If so, i want to display a image: 'ok'. If not, i want to dispaly an image: 'not ok' document.getElementById("checkemail").innerHTML = xmlhttp.responseText ; ok, if it's unique and a correct email pattern i get as xmlhttp.responseText --> true if not --> false BUTTTTTTT this code to display not those words true of false doesnt work for some reason :s var correct = xmlhttp.responseText; if (correct){ document.getElementById("checkemail").innerHTML = '<img src="Images/true.gif" border="0" alt="ok" />'; } else { document.getElementById("checkemail").innerHTML = '<img src="Images/false.gif" border="0" alt="nok" />'; } } IT always displays the good image, no matter what Quote Link to comment https://forums.phpfreaks.com/topic/260949-i-feel-so-dumb/ Share on other sites More sharing options...
RickXu Posted April 14, 2012 Share Posted April 14, 2012 var correct = xmlhttp.responseText; if (correct){ document.getElementById("checkemail").innerHTML = '<img src="Images/true.gif" border="0" alt="ok" />'; } else { document.getElementById("checkemail").innerHTML = '<img src="Images/false.gif" border="0" alt="nok" />'; } } IT always displays the good image, no matter what If your 'correct' is returned as string, the if statement would always be true. Try: if(correct == 'true') or try to handle it in a different way. Quote Link to comment https://forums.phpfreaks.com/topic/260949-i-feel-so-dumb/#findComment-1337408 Share on other sites More sharing options...
stijn0713 Posted April 15, 2012 Author Share Posted April 15, 2012 if (correct = "true") or if (correct ='true') both dont work ... OMG what's happening :S please help... what's wrong with the syntax, why is it not interpreted well? Quote Link to comment https://forums.phpfreaks.com/topic/260949-i-feel-so-dumb/#findComment-1337608 Share on other sites More sharing options...
stijn0713 Posted April 15, 2012 Author Share Posted April 15, 2012 ok i fixed it like this, but if somebody could help clarify me for further reference it would still be very appreciated. Apparently this is not the same: CASE 1 xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { document.getElementById("checkemail").innerHTML = xmlhttp.responseText; } } And in php script $correct = '<img src="Images/slecht.gif" border="0" alt="ok" />'; $email = $_GET['email']; if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) { $nietemailvorm = true; } else { $nietemailvorm = false; } $sql = mysql_query("SELECT email FROM respondenten WHERE email= '$email' "); if (mysql_num_rows($sql) != 0 || $nietemailvorm) { $correct = '<img src="Images/goed.gif" border="0" alt="nok" />'; } echo $correct; CASE 2 var correct = xmlhttp.responseText; if (correct == "true"){ document.getElementById("checkemail").innerHTML = '<img src="Images/slecht.gif" border="0" alt="ok" />' } else { document.getElementById("checkemail").innerHTML = '<img src="Images/goed.gif" border="0" alt="nok" />'; $correct = 'true'; $email = $_GET['email']; if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)) { $nietemailvorm = true; } else { $nietemailvorm = false; } $sql = mysql_query("SELECT email FROM respondenten WHERE email= '$email' "); if (mysql_num_rows($sql) != 0 || $nietemailvorm) { $correct = 'false'; } echo $correct; Quote Link to comment https://forums.phpfreaks.com/topic/260949-i-feel-so-dumb/#findComment-1337642 Share on other sites More sharing options...
RickXu Posted April 15, 2012 Share Posted April 15, 2012 Both cases are technically not wrong but really depends on what your ajax request returns. For stuff like this, you could use jQuery to handle the ajax. 100% worry free and it would look more professional too. But still you need to be clear what you expect to receive from the ajax request. Quote Link to comment https://forums.phpfreaks.com/topic/260949-i-feel-so-dumb/#findComment-1337670 Share on other sites More sharing options...
gizmola Posted April 16, 2012 Share Posted April 16, 2012 I personally believe it is best to target the most specific element you can. In this case all you are trying to change is the image.src attribute. From a pure efficiency standpoint, it is much more efficient for you to return true/false than to return the html for the entire image, and to simply change the .src attribute in your javascript. Quote Link to comment https://forums.phpfreaks.com/topic/260949-i-feel-so-dumb/#findComment-1337704 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.