views:

86

answers:

2

I would like to send data using jQuery ajax API

var myData = {"param1" : $('#txtParam1').val(), "param2" : $('#txtParam2').val()};

   $.ajax({
        url: 'DataService.php?action=SomeAction',
        type: 'POST',
        data: myData,
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(result) {
        alert(result.Result);}
   });

When I tried to retrieve this data on php using

    $param1 = $_REQUEST['param1'];

$param1 is showing null and print_r($_REQUEST) is only showing action = SomeAction ..

How to retrieve the posted data on php page ?

+1  A: 

Try this:

 $params = json_decode($_POST['param1']);

And then check what you have got:

var_export($params);

Or you can use foreach loop:

foreach($params as $param)
{
  echo $param . '<br />';
}

You are using POST, method, dont use REQUEST because it is also less secure.

Sarfraz
Sarfraz its not working .. Have u tried at your side ?
Nav Ali
@Nav Ali: try removing the dataType: 'json' line from your code and then see.
Sarfraz
can get the data when I removed content-type
Nav Ali
A: 

Try using

$param1 = $_POST['param1'];

amindzx