如何在不单击提交的情况下在同一页面上获取选择值

时间:2015-09-22 17:42:11

标签: php html

<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值通过选择选项但不点击提交按钮。任何建议将不胜感激。

2 个答案:

答案 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'];
?>