简体   繁体   English

将条件下拉列表写入MySQL

[英]Writing Conditional Drop Downs to MySQL

Hopefully this isn't too confusing.. 希望这不会太令人困惑。

I have a conditional form that has the user select a category, based on that category they'll need to choose from that category's sub categories. 我有一个条件表格,可以让用户选择一个类别,然后根据该类别,他们需要从该类别的子类别中进行选择。 This works fine. 这很好。

However, my issue is when writing to MySQL the Value that's inserted in my Sub Category column is always the last Select group's first Value (in this case it's "sandwich"). 但是,我的问题是,在写入MySQL时,在“我的子类别”列中插入的值始终是最后一个“选择”组的第一个值(在本例中为“三明治”)。 Example.. 例..

My Main Categories: Starters | 我的主要分类:入门| Supper | 晚饭 Sandwiches 三文治

  • Starters' Sub Categories: Coastal or Southern 起动器的子类别:沿海或南部
  • Supper's Sub Categories: Smokehouse or Specialties 晚饭的子类别:烟熏房或特色菜
  • Sanwiches' Sub Categories: Sandwich or Po-Boy 三明治的子类别:三明治或男孩

No matter which Category/Sub Category you select, it always writes the "Sandwich" subcategory in MySQL. 无论您选择哪个类别/子类别,它始终会在MySQL中写入“三明治”子类别。 Make sense? 说得通?

So here's my code & JSFiddle if you want to play with the dropdowns. 如果您想使用下拉菜单,这是我的代码和JSFiddle。 http://jsfiddle.net/kkobayashi/5f5tw4t0/ http://jsfiddle.net/kkobayashi/5f5tw4t0/

PHP to MySQL PHP到MySQL

<?php
    if( isset( $_POST['create'] ) ):

    $cat = $_POST['cat']; 
    $catsubs = $_POST['subcategory']; 
    $name = $_POST['name'];
    $description = $_POST['description'];
    $price = $_POST['price'];

    mysql_query("INSERT INTO leDB (cat,subcategory) 
        VALUES('$cat','$catsubs')") 

    or die(mysql_error());

    echo "Success."; /** success message **/

    endif;
?>

HTML 的HTML

<form action="" method="POST">
<!-- MAIN CATS -->
<select id="mainCat" class="source" name="cat">
    <option value="starters">Starters</option>
    <option value="supper">Supper</option>
    <option value="sandwiches">Sandwiches</option>
</select>


<!-- SUB CATS -->
<div id="cat_starters" class="subcategory" style="display:inline;">
    <select class="" id="starters" name="subcategory">
        <option value="coastal">Coastal</option>
        <option value="southern">Southern</option>
    </select>
</div>
<div id="cat_supper" class="subcategory hidden">
    <select class="" id="supper" name="subcategory">
        <option value="smokehouse">Smokehouse</option>
        <option value="specialties">Specialties</option>
    </select>
</div>
<div id="cat_sandwiches" class="subcategory hidden">
    <select class="" id="sandwiches" name="subcategory">
        <option value="sandwich">Sandwich</option>
        <option value="poboy">Po-Boys</option>
    </select>
</div>
</form>

JS JS

// Conditional Drop Down
    $(document).ready(function(){
        $('#mainCat').on('change', function() {         
            // Setting this variable to add inline
            var inline = document.querySelector('#cat_' + $(this).val() );

            // Show/Hide
            $('div.subcategory').hide();
            $('#cat_' + $(this).val() ).show();
            inline.style.display = "inline";
        });
    });

A little CSS 一点CSS

div.hidden { 
   display: none; 
}

Don't know how well of a job I did describing the issue, if you're confused feel free to yell at me. 如果您感到困惑,请随时对我大喊大叫,不知道我对这个问题的描述做得如何。 Thanks y'all. 谢谢大家。

Hiding the select does not actually remove it from the DOM, as you would see if you would check the source. 隐藏选择并不会真正将其从DOM中删除,就像您将查看是否检查源一样。 It only hides it from the user. 它只对用户隐藏。

This means that every select with the same name attribute will override the last one, hence why you only get the last one. 这意味着具有相同name属性的每个选择都将覆盖最后一个选择,因此为什么只获得最后一个选择。 an easy fix could be to add the name attribute to the correct select. 一个简单的解决方法是将name属性添加到正确的选择中。

$(document).ready(function(){
    $('#mainCat').on('change', function() {         
        // Setting this variable to add inline
        var inline = document.querySelector('#cat_' + $(this).val() );

        // Show/Hide
        $('div.subcategory').hide().attr('name', '');
        $('#cat_' + $(this).val() ).show().attr('name', 'subcategory');
        inline.style.display = "inline";
    });
});

you need to add unique names to your selects. 您需要为选择添加唯一的名称。 For example for starters, add name="starters" . 例如,对于初学者,请添加name="starters" Add names as the option values of the category. 添加名称作为类别的选项值。

And after that, use: $catsubs = $_POST[$cat]; 然后,使用: $catsubs = $_POST[$cat];

Change the subcategory names to maybe subcategory1, ...2 and ....3 or just make them unique in the form 将子类别名称更改为subcategory1,... 2和.... 3,或者仅在形式中使它们唯一

You need the names to be unique. 您需要名称唯一。 The reason you end up with the same sandwich no matter what you select is because you are using the same name. 无论选择什么,最终都使用相同的三明治的原因是因为您使用的是相同的名称。 When the $_POST data is sent, it sends an array with the key equal to the name in the form. 发送$ _POST数据后,它将发送一个键名与表单中名称相同的数组。 So to know exactly what a user clicked, selected or typed, each name must be unique 因此,要确切了解用户单击,选择或键入的内容,每个名称必须唯一

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

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