0

I'm trying to figure out how to "send" a Javascript/JQuery variable to a PHP variable. I found this post on here, and tried following some of the stuff there, with no luck. I tried to make the simplest code that I could for this, just as a way to test it. Here it is.

thefilename.php

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<?php
$test = $_POST['test'];
echo $test;
echo "Test Ran";
?>

<input type="button" id="rawr" value="Test"/>

<script>
var somestring = "some string";

//Audio test
$("#rawr").click( function() {
    alert("BUTTON PRESSED");
    $.post("thefilename.php", {test : somestring});
});

</script>

This, in my mind, is a simple way to test it. Maybe I have something wrong... not sure... thats why I'm here. Just looking for the simplest way to pass a variable from Javascript to PHP. Eventually I would like to pass an array as well. Any help with that would be greatly appreciated also.

Community
  • 1
  • 1
mandelbug
  • 1,548
  • 5
  • 20
  • 32
  • i think this code is working unless the path to `thefilename.php` is correct... check you developers tool and you should see the echo part in php in network tab(chrome)..or use a success callback function of post `$.post(...{},function(result){alert(result)})` – bipen Jul 15 '13 at 19:26
  • $.post() is an ajax command check out [http://api.jquery.com/jQuery.post/](http://api.jquery.com/jQuery.post/) for examples on how to use this. – Chris Morrissey Jul 15 '13 at 19:27
  • Thanks @ChrisMorrissey I have looked at this page a few times. Still haven't been able to get anything to work. I though this would be easier than it is. – mandelbug Jul 15 '13 at 19:34

1 Answers1

2

I think you are fundamentally confused about the page lifecycle of PHP versus JavaScript. PHP is a server-side scripting language. When you request a page from your server (by navigating to it, or initiating an AJAX call), the PHP script runs from start to finish and your server returns its output (which in this case is an HTML page). That's it. PHP is done when the page source is delivered to your browser.

JavaScript, on the other hand, is a client-side scripting language. JavaScript operates inside your browser and can make changes to a page once the page is loaded (most notably by altering the DOM).

AJAX provides a somewhat tenuous link between the two worlds. Javascript functions like the .post() in jQuery send an asynchronous request back to the server and tell the server to retrieve a certain file. If that file is PHP, the server typically executes it.

So in your example here's what happens:

  1. You type the URL for thefilename.php into your browser and hit enter
  2. Browser sends a request to the server for thefilename.php
  3. Server recognizes thefilename.php as a PHP file and tells PHP to execute the script
  4. The PHP script runs. Its output is determined by the data contained in the request body ($_POST in PHP). It outputs some HTML code.
  5. The server responds to your request for thefilename.php with the HTML code produced by executing it.
  6. Your browser renders the HTML code for you.
  7. You click the button
  8. JavaScript in your browser sends a request to thefilename.php on your server
  9. Your server recognizes thefilename.php as a PHP file and tells PHP to execute the script
  10. The PHP script runs. Its output is determined by the posted data. It produces some HTML code.
  11. The server responds to your request for thefilename.php with the HTML code produced by executing it.
  12. The .post function receives the HTML response and then renders it inside an alert.

I hope that helps explain what's going on. Most likely you want a separate PHP file for your asynchronous call.