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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.