javascript - 如何将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 存储第二个选择标签。所以,伙计们,如果你能在这里帮助我。非常感谢...
解决方案
推荐阅读
- swift - 如何在初始化期间将本地函数传递给另一个对象?
- c++ - 函数模板中的模板模板参数问题
- android-management-api - 无法使用企业帐户发布自托管私有应用程序
- flutter - 如何在 Flutter 中实现这个时间选择器 UI?
- android-studio - 为什么在 android studio 更新后出现“manifestOutputDirectory”错误?
- python - Python - PySimpleGUI - 无法选择日历输入
- java - 我有这个错误:“无法将 java.lang.String 类型的对象转换为 com.flashpub.flash.imageSection 类型”,我不知道为什么
- javascript - 如何使用 Electron.js 彼此相邻显示自定义 Windows 消息框按钮
- android - 我怎样才能在android中制作一个完美的正方形?
- ios - 如何在运行时更改 UIMenu 内的 UIAction 的状态?