6 Replies - 397 Views - Last Post: 15 January 2012 - 07:45 PM Rate Topic: -----

#1 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

XMLHttpRequest is not giving me a result.

Posted 15 January 2012 - 06:30 PM

I just got through debugging the Javascript portion to this and it's not giving me any console errors, so that's good.
But I'm not receiving anything through the PHP code.
It's marked-up so you should be able to see my thought process.

<?php 
	if (isset($_POST['email'])) 
	{ 
		// Alright, an email was input. Let's check. 
		if ($_POST['email'].indexOf("@") != null && $_POST['email'].indexOf(".") != null) 
		{ 
			// Nice. It's a valid email address. 
			// It's good. Just stop here, it will return null (hopefully). 
		} 
		else 
		{ 
			echo "Error: Not a valid email address!"; 
		} 
	} 
	else 
	{ 
		echo "Error: No email was given!"; 
	} 
	function checkStr($var) 
	{ 
		$var = strip_tags($var); 
		$var = htmlentities($var); 
		return stripslashes($var); // I wonder if this counters the '@' and '.' character..  
	} 
?> 


I'm a bit of a n00b to PHP, forgive me. But this isn't professional work, it's just testing. Thanks ;).

Is This A Good Question/Topic? 0
  • +

Replies To: XMLHttpRequest is not giving me a result.

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: XMLHttpRequest is not giving me a result.

Posted 15 January 2012 - 06:52 PM

If you don't print anything, PHP won't send anything. That code only prints something when the email is invalid. If it is valid, however, it just does nothing. You must make it do something if you want something returned.



Also, look into the filter_input function. It has a much easier, and reliable, way to validate emails.
$email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);
if ($email) {
    echo "Success! The email is valid.";
}
else {
    echo "Error: The email is invalid.";
}


Was This Post Helpful? 2
  • +
  • -

#3 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: XMLHttpRequest is not giving me a result.

Posted 15 January 2012 - 06:59 PM

Thanks Atli!
But I'm not getting anything still. Nothing is being returned at all.

This is the Javascript portion:

					if (this.responseText != null) 
					{ 
						document.getElementById("email").style.border = "solid green 3px"; 
						document.getElementById("changer1").innerHTML = this.responseText; 
					} 
					else 
					{ 
						document.getElementById("email").style.border = "solid red 3px"; 
						document.getElementById("changer1").innerHTML = this.responseText; 
					}

Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: XMLHttpRequest is not giving me a result.

Posted 15 January 2012 - 07:17 PM

Ahh I see. In your other thread, you posted the whole Javascript portion. There you have this line:
if (this.readystate == 4) 


This is the problem. The property should read: this.readyState. Note that the "S" is upper-case, while in your code it is lower-case.
Was This Post Helpful? 1
  • +
  • -

#5 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: XMLHttpRequest is not giving me a result.

Posted 15 January 2012 - 07:28 PM

You're amazing. You fixed my code. ^_^

I owe you a million thanks.

Wait, but now when I input a valid email address it gives me an error every time... O.o

I just altered the code a small bit.

					if (this.responseText != null && this.responseText.substr(0, 4) == "Nice") 
					{ 
						document.getElementById("email").style.border = "solid green 3px"; 
						document.getElementById("changer1").innerHTML = this.responseText; 
					} 
					else 
					{ 
						document.getElementById("email").style.border = "solid red 3px"; 
						document.getElementById("changer1").innerHTML = this.responseText; 
					}


And the PHP:

<?php 
	$email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);
	if ($email) {
		echo "Nice! The email is perfectly valid. Epic. :)/>";
	}
	else {
		echo "Error: The email is invalid.";
	}
?> 


Still nothing. :\

This post has been edited by SittingonDucks: 15 January 2012 - 07:31 PM

Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: XMLHttpRequest is not giving me a result.

Posted 15 January 2012 - 07:39 PM

Your POST variables don't match. In your Javascript you send the email as "emailadd" but in your PHP you just use my example value: "email".
Was This Post Helpful? 0
  • +
  • -

#7 SittingonDucks  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-December 11

Re: XMLHttpRequest is not giving me a result.

Posted 15 January 2012 - 07:45 PM

Nice, thanks. Got that.

Well that's it then! Everything is working beautifully.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1