Jump to content

i feel so dumb!


stijn0713

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/260949-i-feel-so-dumb/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/260949-i-feel-so-dumb/#findComment-1337408
Share on other sites

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; 

Link to comment
https://forums.phpfreaks.com/topic/260949-i-feel-so-dumb/#findComment-1337642
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/260949-i-feel-so-dumb/#findComment-1337670
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/260949-i-feel-so-dumb/#findComment-1337704
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.