My Ajax application was working fine, until I implemented an if statement in the PHP script... then like a contagious disease it seems that if I do anything to the data in PHP it returns nothing back to the Javascript layer.
All I can do is echo the data back...
For instance the query string I'm sending to the PHP reads...
index.back3.php?form=login&json={"email":"mo@maurice-campobasso.com","password":"asdasdfas"}
and I know its getting there because in th开发者_如何学编程e simplest debugging PHP file (index.back3.php) that I created all I have is a simple echo statement... and it never fails to send back the data to the Javascript file.
when index.back3.php reads
<?php echo $_GET[json]; ?>
the alert that I have triggering off in the javascript reliably spits out the json string.
also when it reads
<?php echo $_GET[form]; ?>
when I get any more complicated than that nothing comes back to the javascript. Even a simple concatenation...
<?php echo ($_GET[form] . $_GET[json]); ?>
...returns nothing!
A simple if...else statement also returns nothing.
<?php
if(!isset($_GET[form]) {
echo "no!";
} else {
echo "yes!";
}
?>
And this important operation also...
<?php
$array = json_decode($GET[json], true);
var_dump($array);
?>
returns nothing.
OK... so just to make sure everything is above board here is my Ajax output function in the Javascript layer.
function responseAjax() {
if (myRequest.readyState == 4) {
if(myRequest.status == 200) {
var foo = myRequest.responseText;
alert(foo);
} else {
alert("An error has occured: " + myRequest.statusText);
}
}
}
Can someone please explain what's going on? I'm truly stumped.
if(!isset($_GET[form]) { echo "no!"; } else { echo "yes!"; }
?>
you are missing a closing parenthesis in the if statement. And as said a few times before, put quotes around your array keys.
While in development, you should also maybe turn on error_reporting to E_ALL. it helps find out what a small error might be.
$_GET['form'] is the right way.
and using form as variable name is not at all good.
use anyother variable and pass $_GET['formname'].
Try to access the php page directly through url and pass the parameters , check first the php
return error or its working propelry.
You can use PHP , jquery and ajax in simple ways.
try to find .post or .get methods in jquery
http://api.jquery.com/jQuery.post/
$.post('ajax/test.html', function(data) { $('.result').html(data); });
I managed to sort it out, it had to do with a line of code which I omitted from this post, because I thought it was reduntant. The full version of the function went like this.
function responseAjax() {
if (myRequest.readyState == 4) {
if(myRequest.status == 200) {
var foo = myRequest.responseText;
var cookieData = eval("(" + foo + ")"); //this was the problem!!
alert(foo);
document.cookie = 'email =' + cookieData.email + '; path=/';
document.cookie = 'firstname =' + cookieData.FirstName + '; path=/';
} else {
alert("An error has occured: " + myRequest.statusText);
}
}
}
It was the 'var cookieData' line... though I'm not sure why.
精彩评论