Q:

Ajax/jQuery POST

I'm constructing a form where a user can choose something from a dropdown box, then a different form is loaded depending on their selection.
For example if they chose "car" it would load a form for buying a car. However if they chose "dog" it would ask type of dog, etc etc.
I'm having trouble understanding the format in which data is passed to a page. For example, if I have a script in a file called "formLoader.php" that uses a variable name "$selection" to query the database and return the parsed form, how does passing code like the snippet below tell the file to set the value of "$selection" to 'car' for example?
$("button").click(function(){
  $.post("formLoader.php",
  {
    selector:"car"
  }
  });
});

In other words, when you pass data, how do you assign that data to a variable in a php file?
Thanks

A:

When submitting data via a form a la:
<form action="something.php" method="POST"> or
<form action="something.php" method="GET">

or via an AJAX call such as
 $.post("something.php", { }); or
 $.get("something.php",{});

the data passed in is available in PHP via the $_POST and $_GET superglobals, respectively (it is also stored in $HTTP_POST_VARS, but that's not a superglobal and is deprecated). So in your example, you'd have the following:
formLoader.php
$selected = $_POST['selector'];
// do something with the the variable
// echo a result, using json_encode is recommended

Now, note that $.post() accepts a callback function that runs, accepting whatever your php script dumps out.  (Easy reference: http://www.w3schools.com/JQuery/ajax_post.asp )
I also recommend you checkout the jquery documentation for $.post ( http://api.jquery.com/jquery.post/ ) as well as the PHP documentation for handling form input ( http://www.php.net/manual/en/reserved.variables.post.php )

