如何获取SELECT OPTION的选定值

时间:2015-02-02 15:08:31

标签: php jquery html mysql

我的代码有问题,我有两个SELECT,第一个是" drop_drop"第二个是" salidhan_ini",它将改变" salidhan_ini"的选项。每次" drop_drop"是变化,它将保持" salidhan_ini"的价值。从使用php的数据库,但我的代码不起作用。

的index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="jquery-1.8.3.min.js"></script>
<script src="jquery-1.11.1.min.js"></script>
<script>
$(document).ready( function() {
    $('#drop_drop').on('change', function (e) {
        $.ajax({
            type: 'POST',
            url:"process.php",
            data:{keyname:$('#drop_drop option:selected').val()},
            success: function(simbag){
                $("#salidhan_ini").html(simbag);
            }
        });
    });

});
</script>
</head>
<body>
<div id="dropdown1">
<select name="taskOption" id="drop_drop">
  <option value="1">What</option>
  <option value="2">When</option>
  <option value="3">Where</option>
</select>
</div>
<div id="dropdown2">
<select id="salidhan_ini">

</select>
</div>
</body>
</html>

process.php

<?php
    $id_man = $_POSt['taskOption'];
    $con=mysqli_connect("localhost","root","","sample_sample");
    // Check connection
    if (mysqli_connect_errno()){
        die ("Failed to connect to MySQL: " . mysqli_connect_error());
    }

    $result = mysqli_query($con,"SELECT * FROM sample_hatak where id = $id_man");
    if(!$result){
        echo 'Error: '. mysql_error();
    }else{
        while($row = mysqli_fetch_array($result))
        {
            echo "<option value=". $row['FirstName'] .">". $row['LastName'] ."</option>";
        }
    }
mysqli_close($con);
?>

我想要实现的是每次&#34; drop_drop&#34;是改变,选择选项的值来自&#34; drop_drop&#34;将进入&#34; process.php&#34;然后它保存所有具有相应id的数据,该id是&#34; drop_drop&#34;的选定选项的值。然后它将返回all作为&#34; salidhan_ini&#34;的选项。 请帮忙!

2 个答案:

答案 0 :(得分:0)

您需要按如下方式填充第二个选择 -

$(document).ready( function() {
    $('#drop_drop').on('change', function (e) {
        $.ajax({
            type: 'POST',
            url:"process.php",
            data:{keyname:$('#drop_drop option:selected').val()},
            success: function(simbag){
                $.each(simbag, function(val, text) {
                $("#salidhan_ini").append( $('<option></option>').val(val).html(text) )
              });
            }
        });
    });

});

还只包含一个版本的jQuery

答案 1 :(得分:0)

感谢您的帮助! 我发现了我的问题,问题出在process.php

<?php
    $id_man = $_POSt['taskOption'];
    $con=mysqli_connect("localhost","root","","sample_sample");
    // Check connection
    if (mysqli_connect_errno()){
        die ("Failed to connect to MySQL: " . mysqli_connect_error());
    }

    $result = mysqli_query($con,"SELECT * FROM sample_hatak where id = $id_man");
    if(!$result){
        echo 'Error: '. mysql_error();
    }else{
        while($row = mysqli_fetch_array($result))
        {
            echo "<option value=". $row['FirstName'] .">". $row['LastName'] ."</option>";
        }
    }
mysqli_close($con);
?>

问题是“$POSt”所以我将其更改为“$_POST”,

echo "<option value=". $row['FirstName'] .">". $row['LastName'] FirstNameLastName不属于我的数据库中的属性, 因为它是firstnamelastName

谢谢!