gpt4 book ai didi

javascript - 如何将表单内容从 JavaScript 转换为 PHP?

转载 作者:行者123 更新时间:2023-12-02 17:35:46 27 4
gpt4 key购买 nike

我正在尝试将内容从 JavaScript 获取到 php。基本上,我想从下拉菜单中获取 $category 和 $subcategory 中选定值的 List1 和 List2。我根本不懂 javascript,所以这可能是我想做的一件非常基本的事情。

        <script type="text/javascript">

var categories = [];categories["startList"] = ["Apparel","Books"]
categories["Apparel"] = ["Men","Women"];
categories["Books"] = ["Biography","Fiction","Nonfiction"];
categories["Men"] = ["Shirts","Ties","Belts"];
categories["Women"] = ["Blouses","Skirts","Scarves"];
categories["Biography"] = ["Contemporay","Historical","Other"];
categories["Fiction"] = ["Science","Romance"];

var nLists = 2; // number of lists in the set

function fillSelect(currCat,currList){
var step = Number(currList.name.replace(/\D/g,""));
for (i=step; i<nLists+1; i++) {
document.forms[0]['List'+i].length = 1;
document.forms[0]['List'+i].selectedIndex = 0;
}
var nCat = categories[currCat];
for (each in nCat) {
var nOption = document.createElement('option');
var nData = document.createTextNode(nCat[each]);
nOption.setAttribute('value',nCat[each]);
nOption.appendChild(nData);
currList.appendChild(nOption);
}
}

function getValue(L2, L1) {
alert("Your selection was:- \n" + L1 + "\n" + L2 );
}

function init() {
fillSelect('startList',document.forms[0]['List1'])
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

</script>
</head>

<body>
<form action="">
<select name='List1' onchange="fillSelect(this.value,this.form['List2'])">
<option selected>Make a selection</option>
</select>

&nbsp;
<select name='List2' onchange="getValue(this.value,this.form['List1'].value)">
<option selected >Make a selection</option>
</select>
</form>

</body>
</html>
.
.


input type="Submit" name="Send" value="Search">
</form>

</body>
</html>
<?php

$response = array();
require_once 'Test21/funciones_bd.php';
$db = new funciones_BD();
$category = $_POST["List1"];
$subcategory = $_POST["List2"];
.
.
.
?>

最佳答案

您必须将表单提交到同一页面。这可以使用标准表单提交或通过 AJAX 来完成。 JavaScript 是客户端语言,而 PHP 是服务器端语言。这意味着可以在 JavaScript 中操作数据而无需向服务器发送请求,但使用 PHP 访问数据则需要服务器请求。

使用 AJAX 可以避免页面刷新,并且可能会提供更好的用户体验。有关 AJAX 的更多信息,visit here或者只是在 Google 上搜索“Javascript AJAX”。

关于javascript - 如何将表单内容从 JavaScript 转换为 PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22639375/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com