Posted by BJMurphy on 01/16/07 14:54
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]
|