0

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

Vranvs
  • 1,411
  • 4
  • 17
  • 38
  • Via `$_POST['selector'];` -- a basic tutorial in `ajax` and form submission would be helpful. – PeterKA Jul 03 '14 at 03:46
  • Thanks :) I've been reading lots and lots of ajax tutorials but some seem to skip over concepts whereby the writer assumes I'd have that understanding, whereby I don't -- I didn't realize it was passed in a POST array but that makes a lot of sense! – Vranvs Jul 03 '14 at 03:52
  • Artical Refer to : http://stackoverflow.com/questions/10254799/ajax-multiple-drop-downs – it's me Jul 03 '14 at 03:52
  • When you are not sure about jquery API, check jquery official API first, e.g. http://api.jquery.com/jQuery.post/ – Eric Jul 03 '14 at 03:52

4 Answers4

1

Im didn't clear understand what you are thinking but base on my understanding this is my answer:

suppose this is your ajax.

$("button").click(function(){
  $.post("formLoader.php",
  {
    'selector':'car'
  }
  });
});

In your formLoader.php.

$selection = $_POST['selector'];
Hope
  • 644
  • 2
  • 6
  • 21
0

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 )

John C
  • 666
  • 4
  • 8
0

Simply Try this

$("button").click(function(){

$.post("formLoader.php", { selector:"car" },
   function(data) {
            alert(data);
   });

});
Dinesh G
  • 244
  • 1
  • 13
0

First of all, take a look at both documentations: jQuery and PHP

You can post the data like this:

$.post( "test.php", {
    name: "John", time: "2pm" 
});

then, using the PHP reference, you use it like this in server-side (php file that received the POST request):

<?php
$myname = $_POST['name']
?>

the result will be in yout variable called $myname.

Rafael Brasil
  • 232
  • 1
  • 6