Json

Json

Hi,

I've hit a brickwall with some ridulously simple jquery and i'm about to flip my desk so I hope someone can help. I've been trying to figure this out for hours and it makes no sense to me.

I'm simply trying to return a PHP variable to an via an Ajax call:

This is my js:

  1. $(function(){
  2.               
  3.      $('#button').click(function(){
  4.          
  5.           var user=1;
  6.          
  7.           $.ajax({
  8.                type: "POST",
  9.                dataType: "json",
  10.                data: "user",
  11.                url: "test.php",
  12.                success:function(result){
  13.                     alert('hello '+result['success']);
  14.                }
  15.               
  16.           });
  17.          
  18.      return false;
  19.      
  20.      });
  21.    
  22. });
This is my PHP:

  1. <?
  2. $my_data=array("failed"=> 'No!','success'=>'Hooray!');
  3. echo json_encode($my_data);
  4. ?>
My index page links to the jquery library as follows:

<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

And to my js:

<script type="text/javascript" src="my_test.js"></script>

The Ajax is called when I click the button of an html form, but it doesn't give me the alert. If II comment out the dataType:json line the alert does appear albeit with the variable data missing.

It could not be any more elementary could it? Yet this has caused me endless wasted hours trying hundreds of combinations but no matter what I do I can't get the alert to popup with the data returned from the php.

Please help!

Thank you.

Johnny.