sam sam - 1 year ago 83
Javascript Question

Post jQuery variable on same file and receive with PHP

How can I post jQuery data and receive on the same file without passing through the URL as a variable (eg.


For example here's the that I can see in the console log.

function (response) {

I want to post that data and receive on the same file. I have tried following:

function (response) {
$.post(window.location.href, {json_data:});

but when in the body of same file I print


it does not display anything.

Answer Source

Make sure that is Type: PlainObject or String. See more here on PlainObjects


Here is a video that describes what this code sample does.

Code Sample

  if (empty($_POST)) :
      Nothing was posted, please click there \/<br><br>
  else :
    echo 'You posted: '.print_r($_POST['json_data'], 1).'<br><br>';

<a href="javascript:" onclick="fake_simple_response('simple string');">click here (simple string)</a><br>
<a href="javascript:" onclick="fake_simple_response({cat:'meow', dog:'woof'});">click here (plain object)</a>

<script src=""></script>
function fake_simple_response (response) {
    $.post(window.location.href, {json_data: response});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download