0

I have the following ajax script

 dataString = 'cipher'; //
    var jsonString = JSON.stringify(dataString);
       $.ajax({
            type: "POST",
            url: "tokenize.php",
            data: {data : jsonString}, 
            cache: false,

            success: function(){
                alert("OK");
            }
        });
       returnedvalue = result //I wanted to store the value returned by php in this variable
       alert(returnedvalue);

and the tokenize.php is

$data = json_decode(stripslashes($_POST['data']));
return $data;  //Pass this value as ajaxs response

But im not able to get this.When I checked in the console I get error uncaught:result is not defined.

Im new to query,searched on google and done upto this.

The json is not necessary,all I wanted to do is pass a value to php and process it and give a rssponse back to teh javascript so that i can use it in the javascript

Piya
  • 1,134
  • 4
  • 22
  • 42

2 Answers2

2

You are passing just string(dataString = 'cipher';) into ajax file. There is no need to JSON.

To use echo for return values from AJAX file.

Update in JS:

dataString = 'cipher'; //

       $.ajax({
            type: "POST",
            url: "tokenize.php",
            data: {data : dataString}, 
            cache: false,

            success: function(result) { //just add the result as argument in success anonymous function
                var returnedvalue = result;
                alert(returnedvalue);
            }
        });

Update in PHP file:

$data = stripslashes($_POST['data']);
echo $data; 
kpmDev
  • 1,330
  • 1
  • 10
  • 28
1

You need to pass the parameter into the anonymous function for the success event.

success: function(data) {
    returnedvalue = data;
    console.log(data); //alert isn't for debugging
} 
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
  • 1
    @PiyaSharma Is this works with return $data;,I think ou need to change it to echo $data; in your php file also – Shijin TR Mar 30 '14 at 07:55