[英]How do I get a form contents from JavaScript to PHP?
我正在嘗試從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>
<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的更多信息, 請訪問此處或只是在Google上搜索“Javascript AJAX”。
得到此功能發布而不是警報。 您需要包含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 } );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.