简体   繁体   English

jQuery / PHP / MySQL提交隐藏的选择下拉列表

[英]jQuery / PHP / MySQL Submitting hidden select dropdowns

I have the following code I am using to populate a second dropdown box after the first dropdown box is selected (actually they are all already made, it just reveals the correct one based on your first dropdown: 在选择第一个下拉框后,我有以下代码用于填充第二个下拉框(实际上它们已经完成,它只是根据您的第一个下拉列表显示正确的一个:

<tr><th>Customer</th><td>
        <select name='customer_id' id='customer_id'>
            <option value=''>-- Select Customer --</option>
            <?php
            $results = Misc::customerDropDown();
            while ($row = mysql_fetch_array($results)) {
                echo "<option value='" . $row['customer_id'] . "'>" . $row['customer_name'] . "</option>";
            }
            ?>
        </select><span class='error'>&nbsp;*&nbsp;<?php echo $errors['customer_id']; ?></span>
    </td></tr>
<?php
$results = Misc::customerDropDown();
$num_customers = mysql_num_rows($results);
$n = 1;
while ($row = mysql_fetch_array($results)) {
    ?>
    <tr class='locations' id='locations<?= $row['customer_id'] ?>'><th>Customer Location</th><td>
            <select name='customer_location<?= $n ?>'>
                <option value=''>-- Customer Locations --</option>
                <?
                $location_results = Misc::locationDropDown($row['customer_id']);
                while ($option = mysql_fetch_array($location_results)) {
                    echo "<option value='" . $option['location_id'] . "'>" . $option['location_name'] . "</option>";
                }
                ?>
            </select><span class='error'>&nbsp;*&nbsp;<?php echo $errors['customer_location']; ?></span></td></tr>                                   
    <? $n++;
} ?> 

The problem that I'm having is that no matter what I've tried the last 'customer_location' dropdown is the one that always gets used because when it submits it's (I believe) overwriting the other two. 我遇到的问题是,无论我尝试过什么,最后一次'customer_location'下拉列表总是被使用,因为当它提交它时(我相信)会覆盖其他两个。

I tried making customer_location an array (customer_location[]) but that just makes it think there are (for example) 3 arrays instead of an array with 3 elements. 我尝试使customer_location成为一个数组(customer_location []),但这只是让它认为有(例如)3个数组而不是3个元素的数组。 I also tried (as shown) naming the location by tacking on a $n to the end of each one but then when I go to reference the post I don't know how as I can't reference $_POST['customer_location$n'] 我也尝试(如图所示)通过在每个结尾处加上$ n来命名位置但是当我去参考帖子时我不知道如何我不能引用$_POST['customer_location$n']

Ideas? 想法?

Whether the dropdowns are visible or not, they are still apart of the form. 无论下拉列表是否可见,它们仍然是形式的一部分。 And their values will get posted back to the server on submit. 并且他们的值将在提交时回发到服务器。 Since you are using logic on the client side to determine which dropdown will be shown, you are going to have to use the same logic on the server side to determine which dropdown's value to get. 由于您在客户端使用逻辑来确定将显示哪个下拉列表,因此您将不得不在服务器端使用相同的逻辑来确定要获取的下拉列表值。

You can reference $_POST['customer_location1'] , or $_POST['customer_location2'] , etc. 您可以引用$_POST['customer_location1']$_POST['customer_location2']等。

I also noted a typo. 我也注意到了一个错字。 The last portion should look like this. 最后一部分看起来应该是这样的。

<?php
    $results = Misc::customerDropDown();
    $num_customers = mysql_num_rows($results);
    $n=1;
    while($row = mysql_fetch_array($results)){
        ?>
        <tr class='locations' id='locations<?=$row['customer_id']?>'>
                <th>Customer Location</th>
                <td>
                    <select name='customer_location<?=$n?>'>
                        <option value=''>-- Customer Locations --</option>
                        <?
                        $location_results = Misc::locationDropDown($row['customer_id']);
                        while($option = mysql_fetch_array($location_results)){
                            echo "<option value='".$option['location_id']."'>".$option['location_name']."</option>";
                        }
                        ?>
                    </select>
                    <span class='error'>&nbsp;*&nbsp;<?php echo $errors['customer_location'.$n]; ?></span>
                </td>
            </tr>
        <?
        $n++;
    }
?>

The closing td and tr need to be in the loop. 关闭tdtr需要在循环中。 And the $errors should probably include $n . $errors应该包括$n

EDIT 编辑

For example, dropdown 1 looks like this: 例如,下拉列表1如下所示:

<select id='dd'>
   <option value='1'>1</option>
   <option value='2'>2</option>
   <option value='3'>3</option>
</select>

In your jquery you've got a change handler that shows the right dropdown (this part will really only work in this exact example, but you said you already had this part working, so I'm glossing over it... basically 'do logic to show correct dropdown'). 在你的jquery中你有一个改变处理程序,显示正确的下拉列表(这部分实际上只适用于这个确切的例子,但你说你已经有了这个部分工作,所以我正在掩盖它......基本上'做'逻辑显示正确的下拉列表')。

$("#dd").change(function() {
   var dd = $(this).val();
   $("#dd1").hide();
   $("#dd2").hide();
   $("#dd3").hide();
   $("#dd" + val).show();
});

Then you've got your three dropdowns that are hidden: 然后你有三个隐藏的下拉菜单:

<select id='dd1' style='display: none;'>
   <option value='1'>1</option>
   <option value='2'>2</option>
   <option value='3'>3</option>
</select>
<select id='dd2' style='display: none;'>
   <option value='1'>1</option>
   <option value='2'>2</option>
   <option value='3'>3</option>
</select>
<select id='dd3' style='display: none;'>
   <option value='1'>1</option>
   <option value='2'>2</option>
   <option value='3'>3</option>
</select>

Then you select option 2 in #dd . 然后在#dd选择选项2。 #dd2 is shown, then you select option 1, and click submit. #dd2 ,然后选择选项1,然后单击“提交”。 Then in your php code (my php may be a bit rusty): 然后在你的PHP代码(我的PHP可能有点生疏):

$val = $_POST['dd'];
$ddval = '';

if ($val == '1') {
   $ddval = $_POST['dd1'];
} else if ($val == '2') {
   $ddval = $_POST['dd2'];
} else if ($val == '3') {
   $ddval = $_POST['dd3'];
} 

Based on a tip from @Barmar (see comments) I was able to piece together the solution to find and assign the value of the needed input with the following: 基于来自@Barmar的提示(请参阅注释),我能够将解决方案拼凑在一起,以便使用以下内容查找并分配所需输入的值:

// Figure out which customer_location$n to use
                foreach ($_POST as $key=>$value){ 
                 if (preg_match('/^customer_location/', $key)){ 
                  if($value!=NULL){ $location = $value; }
                 }
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM