views:

33

answers:

4

Hi, i am using a dropdown in php which asks for user_category before creating the user. Upon selection of the category further text boxes are to be created dynamically depending upon the selection. I know how to get the selected value from $_POST but here i need it befor form submission.Pls help

+1  A: 

You cannot do that ONLY with PHP. PHP is a server-side language, so once you request the page and the PHP interpreter on the server interpreted it and served you the HTML, it does not know anything about what happens to the client. So PHP cannot know if the client changed selection.

What you can do is use JavaScript to do that.

You have 2 options:

1) have the text boxes values that you want to show hard-coded in Javascript for all the cases

2) query a PHP file asynchronously using AJAX so that it will return only the needed values.

nico
+1  A: 

You would need to use js. Either:

document.getElementById('elementName').value

or

document.formName.elementName.value

or

use jQuery.

Than if you need a response from a php script before submission you would need to use ajax.

Babiker
After I get the value of the selection using document.getElementById('name').value How will I use this value in my php code ..eg if(user_category == 'admin') then i need to place one text box in my form .
aeonsleo
If you need to do this whole thing without a refresh then you can only use AJAX.
Babiker
A: 

You can not get them in php before submission; you may also consider something like simple html dom. Or you can get them with javascript also with something like document.getElementById

Sarfraz
+1  A: 

you can make this in steps like this: so after the user clicks the submit button right after the dropdown. now you have users choice in the $_POST and you can display the appropriate form. or you do it with js, like nico sad.

antpaw