0

I am current have the following script. I want pass a value from javascript to php by using AJAX. What's wrong with my code?

<script type=" text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
      $.post("index.php",{host:document.referrer},function(data){});  
    });
</script>
<?php
$dataString=$_POST['host'];
echo $dataString;
?>
1
  • theres a lot wrong with it. it is not complete
    – Ryan
    Commented Dec 9, 2012 at 3:38

1 Answer 1

2

Since the PHP is executed first, you will never see the echo $dataString from your AJAX request. This code will post your request to the server, but you'll never see the result.

Right now here is what is happening:

  1. Your web server renders out your page.
  2. Your browser posts a request to index.php, and ignores the result

From the docs you can see this:

$.post('index.php', function(data) {
  $('.result').html(data);
});

The data in that function will return what echo $dataString; outputs from your script.

Also, your post isn't configured correctly. You need to put data: before {host:document.referrer}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.