Page 1 of 1
form updates after value select?
Posted: Fri Jan 22, 2010 4:51 am
by filtered
Hi there,
Im just learning PHP so please bare with me! What i think im trying to do is simple i think, yet i cant work out how to do it! Basically i have a form that will have a drop selection asking the user how many people want to reserve a table at a restaurant (its for group bookings), so 8, 9, 10 people etc.. then what i would like to happen is either after clicking submit or automatically, a form is generated that displays a number of text boxes for what ever number of people was selected.
So for example. The user selects '8' from the drop down, stating they have 8 people in their group, and then form generates 8 sets of 'mini forms' asking what they want each persons name is, what they for a starter, main and dessert, and any special requirments.
Does that make sense? sorry if im not making myself clear enough! Any help or pointers would be HUGELY appreciated!
Many thanks
Tim
Re: form updates after value select?
Posted: Fri Jan 22, 2010 7:10 am
by hairytea
use javascript and event handlers.....
html...
Code: Select all
<form name="myForm" id="myForm" action="." method="post">
<select name="numOfPeople" id="numOfPeople" onchange="howMany();">
<option value="How many people?" selected="selected">How many people?</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<br/>
<?php echo $displayTextFields; ?>
</form>
javaScript...
Code: Select all
<script type="text/javascript">
<!--
function howMany() {
var selectLen = document.myForm.numOfPeople.length;
for (var i = 0; i < selectLen; i++) {
if (document.myForm.numOfPeople.options[i].selected) {
var selected = document.myForm.numOfPeople.options[i].text;
if (selected == "How many people?") {
alert("Please choose how many people to book in from the drop down list!");
document.myForm.numOfPeople.focus();
} else {
window.location = "index.php?people=" + selected;
}
}
}
}
//-->
</script>
explanation....
1: The event handler (onchange="howMany();") calls the function which checks to see which option is selected when an option is chosen.
2:The function loops through the select list to find which one is in fact selected and then reloads the page passing a variable that can be collected by php. Example... index.php?people=3
3: All you need to in the php script is use the following...
Code: Select all
$numOfPeople = $_GET['people']; (collect the number of people chosen and store it in the variable $numOfPeople)
if ($numOfPeople == 1) {
$displayTextFields = "
<p>Name:<br/>
<input type="text" name="name" size="20" />
</p>
<p>Starter:<br/>
<input type="text" name="name" size="20" />
</p> ETC ETC ETC......
";
}
else if ($numOfPeople == 2) {
code to execute
}
}
else if ($numOfPeople == 3) {
code to execute
}
4: The $displayTextFields variable will set with the amount of form / text fields necessary (you need to code that in (example given if $numOfPeople equals 1)).
The html has this = <?php echo $displayTextFields; ?> | which will output the results and add the text fields necessary to the form.
happy coding
N.B.
In my opinion this is a great way to do it as javaScript will perform these actions using the clients pc and taking away unnecessary load and work from the web server.
Re: form updates after value select?
Posted: Fri Jan 22, 2010 7:36 am
by hairytea
oh and just for future reference....
Don't start a thread with 'I am new to PHP' or 'I am learning PHP' etc etc. As this instantly puts people off helping due to the fact they will read it and think....'yeah can give advice, but not writing the whole script for you' lol
I learnt that the hard way when I needed forums back when I started coding about a year and a half ago
Although saying that I have pretty much written it all for you anyway lmao
The point i'm trying to make really is...
Try to code it yourself and if it doesn't work add the code to your post and ask where gone wrong.
Re: form updates after value select?
Posted: Fri Jan 22, 2010 8:52 am
by filtered
Hey Hairy
Thankyou so much for your reply, i was halfway there on coding it, but your explanation totally helps! thankyou! (and thanks for the tips!
Tim