我正试图从一个php页面的下拉列表中发布一个选定的项目,以便在另一个php页中访问。页面没有提交按钮,所以我在页面底部使用javascript。这是应该将该项目张贴的页面。
<!DOCTYPE html>
<html>
<head>
<?php session_start();?>
</head>
<body>
<select class="myList" name="Country" id="country-selector" autofocus="autofocus" autocorrect="off" autocomplete="off">
<option value="" selected="selected">Select Country</option>
<option value="Belgium" data-alternative-spellings="BE België Belgie Belgien Belgique" data-relevancy-booster="1.5">Belgium</option>
</select>
<Script>
$(function () {
$("#country-selector").change(function(){
var x = document.getElementById("country-selector").value;
alert(x);
$.post('page2.php', {'selectedCountry': x});
$.post('page2.php','val='+$(this).val(),function (response){
alert(response);
});
});
});
</script>
</body>
</html>
然后我想要page2.php作为目标页面。我转到page2.php并尝试查看如下的post数组:
<?php
session_start();
echo $_POST['selectedCountry'];
print_r($_POST);
?>
但是selectedCountry项目不在$_POST数组中。如何使用javascript将所选项目放入php post数组中
谢谢
$("#country-selector").change(function(){
var x = document.getElementById("country-selector").value;
$.post('page2.php', {'selectedCountry': x}).done(function(response) {
alert(response);
});
});