首页 > 解决方案 > 如何将javascript数组数据插入数据库?

问题描述

我正在尝试制作一个电子商务网站。所以我在这里要做的是我有两个选择标签,在第一个选择标签上选择特定数据后,第二个标签的值应该自动列出。意思是假设,第一个标签用于“部门”,而第二个标签用于类别。因此,如果我在第一个标签中选择“电子产品”,那么电子产品(例如笔记本电脑、智能手机等)内的类别(例如笔记本电脑、智能手机等)应该会自动出现,同样的情况下应该只出现的布料(上衣、下衣等) )。我所做的是我在一个选择标签中创建了部门,而我将类别存储在一个 javascript 数组中。以下是html的代码...

 <select class="products" id="department_select" name="department" onchange="categoryChange(this);">
            <option value="empty">Select</option>
            <option value="Electronics">Electronics</option>
            <option value="Men's clothes">Men's clothes</option>
            <option vlaue="Women's clothes">Women's clothes</option>
            <option vlaue="Home & Kitchen">Home & Kitchen</option>
            <option vlaue="Sports & Fitness">Sports & Fitness</option>
        </select><br>

下面是我将类别存储在 javascript 数组中的代码。

<script>
    // array of possible countries in the same order as they appear in the country selection list 
    var categoryLists = new Array(5)
    categoryLists["empty"] = ["Select"];
    categoryLists["Electronics"] = ["Select","Camera", "Desktop", "Laptop", "Mobile", "Smart watch"];
    categoryLists["Men's clothes"] = ["Select","Foot wear", "Bottom wear", "Top wear", "Summer clothes", "Winter clothes"];
    categoryLists["Women's clothes"] = ["Select","Clothing", "Foot wear", "Saree", "Top wear", "Bottom wear"];
    categoryLists["Home & Kitchen"] = ["Select","Living Room Furniture", "Bedroom Furniture", "Office & Study", "Kitchen needs"];
    categoryLists["Sports & Fitness"] = ["Select","Cricket", "Football", "Gym Accessories"];

    /* CountryChange() is called from the onchange event of a select element. 
    * param selectObj - the select object which fired the on change event. 
    */ 
    function categoryChange(selectObj){
        // get the index of the selected option 
        var idx = selectObj.selectedIndex;
        // get the value of the selected option 
        var which = selectObj.options[idx].value;
        // use the selected option value to retrieve the list of items from the countryLists array 
        cList = categoryLists[which];
        // get the country select element via its known id 
        var cSelect = document.getElementById("category_select");
        // remove the current options from the country select 
        var len = cSelect.options.length;
        while(cSelect.options.length > 0){
            cSelect.remove(0);
        }
        var newOption;
        // create new options 
        for(var i=0; i<cList.length; i++){
            newOption = document.createElement("option");
            newOption.value = cList[i];// assumes option string and value are the same 

            newOption.text = cList[i];
            // add the new option 
            try{
                cSelect.add(newOption);
            }
            catch(e){
                cSelect.appendChild(newOption);
            }
        }
    }

所以通常当我从 php 存储数据时,我使用 $categories = $_POST['categories'] 和所有。但是这里的问题是我似乎找不到将javascript数组数据存储到数据库中的方法。特别是因为我使用 php 存储一个选择标签,而使用 javascript 存储第二个选择标签。所以,伙计们,如果你能在这里帮助我。非常感谢...

标签: javascriptphphtmlsqlarrays

解决方案


推荐阅读