Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将表单内容从JavaScript转换为PHP?_Javascript_Php_Jquery - Fatal编程技术网

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

如何将表单内容从JavaScript转换为PHP?,javascript,php,jquery,Javascript,Php,Jquery,我正在尝试将内容从JavaScript转换为php。基本上是从下拉菜单中选择的值的列表1和列表2,我想在$category和$subcategory中获取。我根本不懂javascript,所以这可能是我正在尝试做的一件非常基本的事情 <script type="text/javascript"> var categories = [];categories["startList"] = ["Apparel","Books"] categories["Apparel"

我正在尝试将内容从JavaScript转换为php。基本上是从下拉菜单中选择的值的列表1和列表2,我想在$category和$subcategory中获取。我根本不懂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"];
.
.
.
?>

var类别=[];类别[“惊奇者”]=“服装”、“书籍”]
类别[“服装”]=“男性”、“女性”];
类别[“书籍”]=“传记”、“小说”、“非小说”];
类别[“男士”]=“衬衫”、“领带”、“腰带”];
类别[“女性”]=[“衬衫”、“裙子”、“围巾”];
类别[“传记”]=“当代”、“历史”、“其他”];
类别[“小说”]=“科学”、“浪漫”];
变量nLists=2;//集合中的列表数
函数fillSelect(currCat,currList){
var step=Number(currList.name.replace(/\D/g,“”);
for(i=步骤;i

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


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

将此函数设置为post而不是alert。您需要包含jquery才能使用$.post

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

function getValue(L2, L1) {
$.post( "urltopage.php", { List1: L1, List2: L2 } );
}

使用jQuery序列化信息,然后通过AJAX调用将其发送到服务器。这意味着我知道jQuery和AJAX。请看这里:如果我不需要级联下拉菜单,我会使用php和html发送到服务器。我尝试过,它可以工作,但因为我需要级联菜单,所以我必须使用javascript。如果你看文档然后,您可以更巧妙地使用.success、.fail等工具,包括jquery,在google上链接到最新的一个,或者将其下载到您自己的应用程序中,只需在包含表单的html页面上添加脚本标记