views:

84

answers:

3

I'm new to JSON/jQuery, and I'm trying various JSON-AJAX examples. Unfortunately, my simple code doesn't seem to be working.

Here is my JS/jQuery in testjs.js:

$(document).ready(function(){ 

$("#radio").click(function () { 

    $.getJSON("testphp.php", { testtest: 'blah' }, function(data){
    alert(data.response);
     });
    });
});

and here is my PHP in testphp.php:

<?
        if(isset($_GET['testtest'])) {
        $arr = array('response'=>'error');
        echo json_encode($arr);
        }
 ?>

    <HTML>
    <HEAD></HEAD>
    <BODY>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
    <script src="testjs.js"></script>
    <input id="radio" type="radio" name="group1" value="radio"> radio<br>
    </BODY>
    </HTML>

When I load testphp.php in my browser, in firebug, I notice that the array is properly returned as {"response":"error"} after I click on the radio button. However, the alert doesn't show up at all...

I've also tried following other similar examples solved here (such as this one: http://stackoverflow.com/questions/1261747/how-to-get-json-response-into-variable-from-a-jquery-script), but these correct solutions also don't produce an alert for me either. I must be doing something really wrong...

Any thoughts are much appreciated. Thanks!

+4  A: 

you should put a die after the json_encode ie

     if(isset($_GET['testtest'])) {
            $arr = array('response'=>'error');
            echo json_encode($arr);
            die();
            }

Or php gonna output the html as well.

RageZ
thanks! that solved the lack-of-alert problem as well!
Elizabeth
any chance you validate my answer by clicking on the tick! Thanks!
RageZ
A: 

sorry -- im a real noob...which tick? i can't vote up this answer, because i don't have a score of 15+...:(

A: 

that last comment was made by me, Elizabeth. would be happy to validate your ans -- just lemme know how. thanks for your help!

Elizabeth