|
Posted by php_Boi on 01/17/07 07:52
I had a similar problem and through much trial and effort i solved it.
I can send you a watered down copy of the solution if you like. Now
Before i begin there are most probably numerous ways to do this and
with that said i didnt exactly do it as u did. my variable that
determined how many passengers,dogs monkey whatever came from another
page that posted but it can easily be done like this(only 3 options to
be brief)
so here is the dropdown on the page passengers.htm
<form method="POST" action="dynamic.php">
<select name="passengers" >
<option value="0">None</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" name="send" value="submit">
Then capture the option in the PHP of the next form called dynamic.php
<html>
//usual web page code here..(<head> etc)
<?php
if (isset($_POST['send']) ) // from previous page * See NOTE below
{
$pass = $_POST['passengers'];
//here is where we dynamically "grow" the form if the # of passengers
is 0 nothing is displayed. if its 1, one passenger with a firstname and
surname field is displayed etc...
for ($counter = 0; $counter < $pass; $counter ++)
{
$offset = $counter + 1;
?>
<form method="post" name="passform" action="dynamic.php" >
<table >
<tr>
<Th >Passenger <?php echo $offset;?></Th>//$offset Variable is for the
Passenger # 1,2,3 etc
</tr>
<tr>
<td >Surname : </td>
<td ><input name="sname[]" type="text" value="<?php echo
$_POST['sname'][$counter];?>">
//this is to store the variable for later processing &validation
//Note the variable name is 'sname[]' to account for an array or number
of sname fields
//value="<?php echo $_POST['sname'][$counter];?>" keeps track of
particular field and in case //there is an error your user doesnt need
to re-fill the form. it is also a means of you to track the //variable
for later processing
</td>
</tr>
<tr>
<tr>
<td >Surname : </td>
<td ><input name="fname[]" type="text" value="<?php echo
$_POST['fname'][$counter];?>"> //this is to store the variable for
later processing &validation
</td>
</tr>
<tr>
<td>
<input name="send" type="submit" value="Send Request">
/*NOTE: This submit button is called 'send' like the previous
page.Thus the selected # of passengers will immediately appear on this
'dynamic.php' form. To do this on one page its very possible just be
carefull about your naming, you'l need to create another form within
the page and submit it somehow. Javascript if onSelect or a submit and
post via PHP(Remember to remove this note its not a proper html comment
but no biggie)*/
</td>
</tr>
<table >
<?php
}
// we opened two braces earlier after we checked if 'send' was set from
previous page to add our
//dynamic content so lets close them
}
?>
//usual code here web page code here..
<html>
The validation of the form can become very tricky once you have
numerous fields that must be added, securely validated,formatted and
submitted. So i wont get into that now. But im sure can figure that
out. As i said i can email you a perfectly functional version with
validation. But this is a starting point
i can also tell you that this
<select name="state<?php echo($i)?>" size="1" >
<option value=""></option>
<option value="AK">AK</option>
<option value="AL">AL</option>
<option value="AR">AR</option>
<option value="AZ">AZ</option>
<option selected value="CA">CA</option>
</select>
will be a problem in case there is need to validate and the page
're-POSTS' to itself coz it wont retain the value.especially if you
have a number of them in an array
<select name="state[]" size="1" >//will be tracked by array hence
"state[]"
<option value="AZ"<?php if (($_POST['Ptitle'][$counter] == '1')) {
echo '
selected="selected"'; } ?>AZ</option>
//etc...
</select>
Hope it helps
Later Timani (php_Boi)
BJMurphy wrote:
> Greg Scharlemann wrote:
> > I'm trying to setup a dyamic dropdown list that displays a number of
> > text fields based on the selected number in the dropdown. The problem
> > I am running into is capturing the data already entered before the list
> > is repopulated. For example, suppose the user selects 3 in the drop
> > down list and 3 text fields are shown. If the user populates the 3 text
> > fields with data and decides to change the drop down to a list of 4 or
> > 5, how do I capture the 3 fields that are entered? It appears that if
> > I include the "Add" button in the same form as the onchange=submit for
> > the drop down list the form gets screwed up. Perhaps there is a better
> > way to do this with php?
> >
> > Thanks for any help...Here's my test file:
> >
> > <?php
> > $showCity = $_REQUEST['showCity'];
> > if(empty($showCity) || $showCity == "") {
> > $showCity = 1;
> > }
> > $cities = array();
> > for($i = 0; $i<$showCity;$i++) {
> > if($_REQUEST['city'.$i] != "") {
> > $cities[$i] = $_REQUEST['city$i'];
> > print "city $i = " . $cities[$i] . "<br>";
> > } else {break;}
> >
> > }
> > print "cities.length = ".count($cities) . "<br>";
> > ?>
> >
> > <html>
> > <body>
> > <table border="0">
> > <form method="post" action="test.php">
> > <tr>
> > <td colspan="4">Define up to 50 City/State combinations.
> > <br/><br/></td>
> > </tr>
> > <tr>
> > <td colspan="4">Insert
> > <select name="showCity" onchange="submit()" >
> > <?php
> > for($i = 0; $i < 50; $i++) {
> > $selected = "";
> > if(($i+1) == $showCity) {
> > $selected = "selected";
> > }
> > ?>
> > <option <?php echo($selected)?> value="<?php echo($i+1)?>"><?php
> > echo($i+1)?></option>
> > <?php
> > }
> > ?>
> > </select> Cities:<br/>
> > </td>
> > </tr>
> >
> > <?php
> > for($i = 0; $i < $showCity; $i++) {
> > ?>
> >
> > <tr>
> > <td>City:</td>
> > <td> <input type="text" name="city<?php echo($i)?>" size="20"
> > value="<?php echo($cities[$i])?>" maxlength="100" /></td>
> > <td align="right"> <b><u>State</u>:</b></td>
> > <td>
> > <select name="state<?php echo($i)?>" size="1" >
> > <option value=""></option>
> > <option value="AK">AK</option>
> > <option value="AL">AL</option>
> > <option value="AR">AR</option>
> > <option value="AZ">AZ</option>
> > <option selected value="CA">CA</option>
> > </select>
> > </td>
> > </tr>
> >
> > <?php
> > }
> > ?>
> >
> > <tr>
> > <td colspan="4" align="center"><br/>
> > <input type="submit" name="submit" value="Add" class="button" />
> > </td>
> > </tr>
> > </form>
> > </table>
> > </body>
> > </html>
>
> The problem is that when you assign the value of the $_REQUEST variable
> to your array, you use single quotes around city$i, so the $i doesn't
> get resolved the way you want. If you use double quotes, like:
>
> $cities[$i] = $_REQUEST["city$i"];
>
> You'll get the behavior you expected.
[Back to original message]
|