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($showC ity) || $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.len gth = ".count($cities ) . "<br>";
?>
<html>
<body>
<table border="0">
<form method="post" action="test.ph p">
<tr>
<td colspan="4">Def ine up to 50 City/State combinations.
<br/><br/></td>
</tr>
<tr>
<td colspan="4">Ins ert
<select name="showCity" onchange="submi t()" >
<?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
}
?>
</selectCities:<b r/>
</td>
</tr>
<?php
for($i = 0; $i < $showCity; $i++) {
?>
<tr>
<td>City:</td>
<td> <inpu t type="text" name="city<?php echo($i)?>" size="20"
value="<?php echo($cities[$i])?>" maxlength="100" /></td>
<td align="right">& nbsp; &nbs p;<b><u>State</u>:</b></td>
<td>
<select name="state<?ph p 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>
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($showC ity) || $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.len gth = ".count($cities ) . "<br>";
?>
<html>
<body>
<table border="0">
<form method="post" action="test.ph p">
<tr>
<td colspan="4">Def ine up to 50 City/State combinations.
<br/><br/></td>
</tr>
<tr>
<td colspan="4">Ins ert
<select name="showCity" onchange="submi t()" >
<?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
}
?>
</selectCities:<b r/>
</td>
</tr>
<?php
for($i = 0; $i < $showCity; $i++) {
?>
<tr>
<td>City:</td>
<td> <inpu t type="text" name="city<?php echo($i)?>" size="20"
value="<?php echo($cities[$i])?>" maxlength="100" /></td>
<td align="right">& nbsp; &nbs p;<b><u>State</u>:</b></td>
<td>
<select name="state<?ph p 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>
Comment