如何处理两个下拉后的值?

时间:2018-10-21 09:19:41

标签: php mysql

默认情况下,我有三个下拉菜单。第一个下拉列表值将影响第二个和第三个下拉列表的可见性。例如,选择选项一,隐藏第三个下拉菜单,显示第二个下拉菜单。第2和第3下拉菜单将保存数据库中同一列的值。

<select class="ui fluid selection dropdown dd" name="category1" id="category1">
    <i class="dropdown icon"></i>
    <option value="">Which category does this fall into?</option>
    <option value="1">Gestures</option>
    <option value="2">Etiquette</option>
</select>
<?php if(isset($_POST['category1'])) { $category1 = $_POST['category1']; }?>

<div class="category2" >
   <select class="ui fluid selection dropdown dd" name="category2" id="category2" >
        <i class="dropdown icon"></i>
        <option value="">Be specific...</option>
        <option value="1">Friendly gestures</option>
        <option value="2">Gestures of respect</option>
        <option value="3">Salutes</option>
        <option value="4">Celebratory gestures</option>
        <option value="5">Finger-counting</option>
        <option value="6">Obscene gestures</option>
        <option value="7">Taunts</option>
        <option value="8">Head motions</option>
        <option value="9">Other gestures</option>
    </select>
</div>
<?php if(isset($_POST['category2'])) { $category2 = $_POST['category2']; }?>

<div class="category3" style="display:none!important;">
    <select class="ui fluid selection dropdown dd" name="category3" id="category3">
        <i class="dropdown icon"></i>
        <option value="">Be specific...</option>
        <option value="10">Greetings</option>
        <option value="11">Making payment</option>
        <option value="12">Visiting someone's house</option>
        <option value="13">Gifts and gift-giving</option>
        <option value="14">Table manners</option>
        <option value="15">Eating and drinking</option>
        <option value="16">Funerals</option>
        <option value="17">Bars and restaurants</option>
        <option value="18">Driving</option>
        <option value="19">Business etiquette</option>
        <option value="20">Hierarchy and honoring the elder</option>
    </select>
</div>
<?php if(isset($_POST['category3'])) { $category3 = $_POST['category3']; }?>

现在,$ category2和$ category3代表数据库中的同一列。如果我要进行插入查询,如何确保任何一个下拉值都已发布并将被存储?

mysqli_query($link,"INSERT INTO practices(category1,category2) VALUES('$category1','$category2 or $category3')")

我尝试过了,

<?php if(isset($_POST['category2'])) { $category2 = $_POST['category2']; }?>
<?php if(isset($_POST['category3'])) { $category2 = $_POST['category3']; }?>

此选项仅适用于其中一个下拉菜单,而错误地适用于另一个下拉菜单。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

将先前发布的值保存在一个隐藏字段中,然后使用第二个下拉列表再次发布。

    <div class="category3" style="display:none!important;">


<?php if(isset($_POST['category2'])) 
{ 
    echo'<select name="pre_category" >
    <option selected="selected" value="'.$_POST['category2'].'">'.$_POST['category2'].' </option> 
    </select>';
 } ?>
    <select class="ui fluid selection dropdown dd" name="category3" id="category3">
        <i class="dropdown icon"></i>
        <option value="">Be specific...</option>
        <option value="10">Greetings</option>
        <option value="11">Making payment</option>
        <option value="12">Visiting someone's house</option>
        <option value="13">Gifts and gift-giving</option>
        <option value="14">Table manners</option>
        <option value="15">Eating and drinking</option>
        <option value="16">Funerals</option>
        <option value="17">Bars and restaurants</option>
        <option value="18">Driving</option>
        <option value="19">Business etiquette</option>
        <option value="20">Hierarchy and honoring the elder</option>
    </select>
</div>

现在您可以从 pre_category

中找到该值