How to access the data from a jquery post in php?

318
December 10, 2016, at 11:03 AM

I'm using the following in my javascript:
jQuery.post( "cmssave.php" , html_to_send , done_edit() , "html" )

And on the PHP end I'm trying to get the posted data using:
$data = $_POST['somevar']

The problem is (I assume) I can't find what post variable is being used.
When I do var_dump($_POST)
I get exactly (in my logs): , referer: http://blablabal
However, when i use: print_r( $_REQUEST, true )
I see all my html that has been sent. I've tried using the following to get the correct data out:
$_POST , $_POST[''] , $_POST[0] , $_POST["data"] , $_POST['html_to_send']
But I get nothing. Am I missing something, or do I need to somehow assign that variable in the javascript? If that's the case how would I do that?

Answer 1

You can define the post variable you want to use in jquery like this:

var data = {
  somevar: 'my post value'
}
jQuery.post( "cmssave.php" , data, done_edit , "html" );

Further you might want to assing the success-function (done_edit) without parentheses, otherwise you would call the success function and assing it's return value.

And in PHP you get the value like this:

$data = $_POST['somevar'];
echo $data;
Rent Charter Buses Company
READ ALSO
How to set Fancybox size per link?

How to set Fancybox size per link?

I want to have multiple links that will use Fancybox but I want to be able to set the width and height for the iframe popup

466
Make an HTTP POST authentication basic request using Javascript

Make an HTTP POST authentication basic request using Javascript

I want to use JavaScript to perform a POST request using the common "Authorization: Basic" methodThe server hosts an OWIN C# App and on successful authentication it should give me a token in JSON format

516
ReferenceError: _ is not defined

ReferenceError: _ is not defined

I am using a jQuery based WordPress Twitter widget and receive the error "ReferenceError: _ is not defined"

653
Jquery ignores my if statement

Jquery ignores my if statement

I am working on a little script that hides the navigation when you scroll down and shows the navigation when scroll up

362