views:

64

answers:

3

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":"[email protected]","password":"asdasdfas"}

and I know its getting there because in the 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.

A: 

$_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); });

zod
Thanks zod, I did what you said and the php file on its own does execute the if statement! The problem must be in the javascript.
cybermotron
did yu change variable name form. dont confuse javascript with form
zod
jQuery is high on my priorities to learn. I think I may read up on it after a nights sleep...
cybermotron
leave jquery..yu ddnt answer about variable name :(
zod
I changed the variable name,but the application still didn't work... I even used quotes $_GET['formname']... The PHP file on its own however did work.
cybermotron
what js error you get.
zod
check error console in ie and mozilla
zod
it says "missing ) in parenthetical - alert(foo);" I don't get it.
cybermotron
A: 
Jonathan Kuhn
Thanks Jonathan, but it still doesn't work.
cybermotron
Oh and I do have error_reporting set to E_ALL... but as I have learned in the answer above the problem appears to be with the Javascript, not the PHP so I'm not getting any errors.
cybermotron
A: 

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.

cybermotron