我有一个名为$ photographerNum的php变量,我希望值的变化取决于我页面上的下拉字段中的值,javascript的样子以及如何接收新值以进行更新php变量?
<!DOCTYPE html>
<html>
<body>
<? $photographerNum = 1; ?>
<p>Select an item from the list.</p>
<select id="photographerNum" onchange="myFunction()">
<option value="Item1">Item1
<option value="Item2">Item2
<option value="Item3">Item3
<option value="Item4">Item4
</select>
<script type="text/javascript">
function myFunction() {
var itemSelected = document.getElementById("photographerNum").value;
document.getElementById("demo").innerHTML = "You selected: " + itemSelected ;
}
</script>
<p id="demo"> - photoNum=<? echo $photographerNum ?></p>
</body>
</html>
答案 0 :(得分:1)
尝试这一点,当您选择一个新项目时,会触发一个表格,输出所选项目的值,之后,您可以发送变量并在服务器端处理它。
<?php
$photographerNum = 1;
echo 'Initial value photographerNum::' . $photographerNum;
?>
<form name="myform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<select name="photographerNum" id="IdphotographerNum" onchange="myform.submit();">
<option value="0">Selecte an item
<option value="1">Item1
<option value="2">Item2
<option value="3">Item3
<option value="4">Item4
</select>
</form>
<?php
if (isset($_POST['photographerNum'])) {
echo 'New value photographerNum::' . $_POST['photographerNum'];
}
?>