<html>
<head>
</head>
<body>
<form action="" method="post">
<select name="state" >
<option value="GOA">1. GOA</option>
<option value="Gujarat">2. Gujarat</option>
<option value="Rajasthan">3. Rajasthan</option>
</select>
<input type="submit" value="Submit">
</form>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$st = $_POST["state"];
echo $st;
?>
</body>
</html>
我希望state
变量中的$st
值通过选择选项但不点击提交按钮。任何建议将不胜感激。
答案 0 :(得分:2)
您可以使用Javascript attach onchange事件处理程序来选择标记
<html>
<head>
</head>
<body>
<form action="" method="post">
<select name="state" onchange="getValue(this)">
<option value="GOA">1. GOA</option>
<option value="Gujarat">2. Gujarat</option>
<option value="Rajasthan">3. Rajasthan</option>
</select>
<input type="submit" value="Submit">
</form>
<script>
function getValue(obj){
alert(obj.value);
}
</script>
</body>
</html>
答案 1 :(得分:0)
<html>
<head>
</head>
<body>
<form action="" method="post">
<select name="state" class="Destination">
<option value="GOA">1. GOA</option>
<option value="Gujarat">2. Gujarat</option>
<option value="Rajasthan">3. Rajasthan</option>
</select>
<input type="submit" value="Submit">
<div class='ShowSelectedValueDiv'>
//Add This Div
</div>
</form>
<script>
$('.Destination').change(function(){
var Destination=$('.Destination').val();
$.ajax({url:"ShowSelectedValue.php?Destination="+Destination,cache:false,success:function(result){
$(".ShowSelectedValueDiv").html(result);
}});
});
</script>
</body>
</html>
制作一个新页面, 的 ShowSelectedValue.php 强>
<?
echo $a=$_GET['Destination'];
?>