Page 1 of 1

Quick question about forms.

Posted: Sun Jun 04, 2006 7:10 pm
by fd9
It seems like I've been struggeling with this for a while. I don't understand why the following code doesn't display 1, 2 or 3 based on the drop-down selection:

Code: Select all

<form name="form1" method="post">
<select name="menu1">
<option value="1">Option1</option>
<option value="2">Option2</option>
<option value="3">Option3</option>
</select>
</form>
 
<?php
$selection = $_POST['menu1'];
echo $selection;
?>
I would like to display 1, 2 or 3 based on the selection without the user having to refresh the page or click a button.

Thanks

ALSO: Is it possible to access a variable that was used in another doucment (I'm working with iframes)

Re: Quick question about forms.

Posted: Sun Jun 04, 2006 7:17 pm
by Christopher
Selects display the text between the <option> and </option> tags for each option, so if you want it to display just numbers then:

Code: Select all

<form name="form1" method="post">
<select name="menu1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
 
<?php
// convert to integer for plain numbers for security
$selection = (int) $_POST['menu1']; 
echo $selection;

// filter to allow only the characters you want for everything else (simple email addresses in this example)
$email = preg_replace('/[^a-zA-Z0-9\_\-\.\@]/', '',  $_POST['email']);
echo $email;
?>

Posted: Sun Jun 04, 2006 7:55 pm
by sheila
The problem is that there is no submit button

Code: Select all

<form name="form1" method="post">
<select name="menu1">
<option value="1">Option1</option>
<option value="2">Option2</option>
<option value="3">Option3</option>
</select>
<input type="submit" value="Go">
</form>
 
<?php
$selection = $_POST['menu1'];
echo $selection;
?>

Posted: Sun Jun 04, 2006 10:08 pm
by fd9
I'm afraid both answers are not what I want. I want the drop-down to say "Option1", etc. but then I want the page to automatically update based on the selection without the user having to click a button or refresh the page. In other words, I want to achieve the same effect from the last post WITHOUT the button. Is that possible?

Posted: Sun Jun 04, 2006 10:52 pm
by mcccy005
It is possible but you have to use javascript or something like that for things like that. Php only executes on the servers computer whereas javascript (and a few other scripting languages) execute on the users computer depending on their actions.
I assume you want the user for example to select a car model (eg. Ford, Holden, Nissan etc) and when they select that, another combo box has a selection of different models for that particular car??
Just did a quick google search but couldn't find anything useful; but I'd say javascript is the way to go

Posted: Sun Jun 04, 2006 11:58 pm
by fd9
I got it to work, except I guess you need to have the updated text shown in a text box. Does anyone know how to modify this so that the user is unable to edit the textbox?

Code: Select all

<center>
<form name="combowithtext">
<select name="example" size="1" onChange="showtext()">
<option value="1">Option1</option>
<option value="2">Option2</option>
<option value="3">Option3</option>
</select>
<p>
<input name="text" type="text" value="0" size="10" maxlength="10" />

<script language="javascript">

var shortcut=document.combowithtext

function showtext(){
shortcut.text.value= shortcut.example.value
}

</script>
</form>
</center>

Posted: Mon Jun 05, 2006 1:44 am
by RobertGonzalez
Quick note...

PHP is a server-side language. Anytime your question relates to the processing of code/data that does not involve submitting, linking or refreshing a page, the answer will be client side. Please post Client Side related topics in the Client Side forum.

To answer your question, you are going to need to have a look at the selectedIndex property of Javascript. I would suggest you google the terms 'select list javascript' and see what comes up. Essentially you need to tell JavaScript to do something using the onChange event of the select form name/id. Then, JavaScript will need to utilize the SelectedIndex property of the passed form value to do what you want to do. This is not exactly it, but something along the lines of...

Code: Select all

<form method="get" name="selectbox" action="">
<select name="choser" onchange="if(this.options[this.selectedIndex].value != -1){ runMyJSFunction(this) }">
<option value="-1">DO NOTHING</option>
<option value="1">Do Something 1</option>
<option value="2">Do Something 2</option>
<option value="3">Do Something 3</option>
<option value="-1">DO NOTHING</option>
</select>
</form>

Posted: Mon Jun 05, 2006 1:47 am
by Christopher
fd9 wrote:Does anyone know how to modify this so that the user is unable to edit the textbox?
There are a number of excellent sites on the web that contain this kind of reference information about each HTML tag, CSS attribute, DOM, Javascript function, etc. -- http://www.w3schools.com is one example.