PDA

View Full Version : Using PHP to get <select> values


rayhuang0924
05-04-2007, 08:51 AM
Dear all:

I'd like to ask how to get <select> value via PHP? (not through submit)
for example:

<select name="testPHP">
<option value="entry 1"></option>
<option value="entry 2"></option>
<option value="entry 3"></option>
</select>

I want to add an event handler "onChange" such that every time I change the selected value (not submit yet), I can pass what I selected to another function for further process (query mysql server and etc.).
I know it's quite easy to achieve via javascript (onChange="function(this.value)"), but how to achieve the same effect via PHP?
Thank you very much!

Ray

SKDevelopment
05-04-2007, 11:31 AM
PHP works at the server side, not at the client side. Without sending any data to the server you can not process the data with PHP.

If you do not want to submit the form, you'll have to use AJAX to interact with the server dynamically.

rafiki
05-04-2007, 12:44 PM
only way i know is submitting

//$_POST['SELECTNAMEHERE'];
$select = $_POST['testPHP'];
//if i selected entry 3
echo $select;
// will echo entry 3

MoD
05-04-2007, 02:23 PM
Easy. Use JavaScript as an OnChange handler, and when the value changes, make a JS that sends VIA AJAX to a php script. Then you get a value back from the script and print it on the screen.

SELECT FIELD:
A
B
C

User selects A:

OnChange -> AJAX -> PHP SCRIPT ... returns A,
document.write(script_result) <-

Read a tutorial about AJAX / XMLHttpRequest, its not that hard.