从选项中获取价值

时间:2014-10-29 20:02:16

标签: php jquery ajax

您好我正在做一些尝试和错误。这是select-option从数据库填充的代码,但这给了我空值

echo "<option  value=\"\">"."Select"."</option>";
$qry = "select * from try where name = '".$_POST['name']."'";
$result = mysqli_query($con,$qry);
while ($row = mysqli_fetch_array($result)){
    echo "<option value='".$row['trynum']."'>".$row['tryname']."</option>";
}

     $.ajax({

            type: "POST",
            url: "json_php_sub.php",
             data: {instructor:$(this).val()},
            datatype: 'json',
            success: function(result){
            alert(result);
                document.getElementById("sub").innerHTML = result;
            }
        });

<select id="sub" name="subb"></select>

我的问题是我是否从下拉列表中选择了内容但没有值。请帮忙..

3 个答案:

答案 0 :(得分:1)

PHP:

 $ajaxAnswer = "<option  value=\"\">"."Select"."</option>";
 $instructor = mysqli_real_escape_string($conn,$_POST['instructor']);
 $qry = "select * from try where name = '".$instructor."'";
 $result = mysqli_query($con,$qry);
 while ($row = mysqli_fetch_array($result)){
   $ajaxAnswer .= "<option value='".$row['trynum']."'>".$row['tryname']."</option>";
 }
 echo $ajaxAnswer;

Jquery的:

$.ajax({
        type: "POST",
        url: "json_php_sub.php",
        data: {instructor:$(this).val()},
        success: function(result){
            $("#sub").html(result);
        }
 });

答案 1 :(得分:0)

data: {instructor:$('#SELECT_ELEMTN_ID').val()},

根据范围和内容,您可能不想使用&#34;这&#34;。

答案 2 :(得分:0)

<强> Jquery的

    
        
        

        $(document).ready(function () {
            $.ajax({
                type: "GET",
                url: "phpfile.php",
                dataType: "json",
                success: function (data) {
                    $.each(data, function (idx, obj) {                            
                        $('#selectdata').append('<option value="'+obj.user_id+'">'+obj.user_name+'</option>' )

                    });
                }
            });
        });
    </script>
</head>
<body>
    <select id="selectdata">

    </select>
</body>

<强> phpfile.php

<?php

$host = "localhost";
$user = "root";
$password ="";
$database= "databasename";

$con = mysqli_connect($host , $user , $password);
$database_connect = mysqli_select_db($con, $database);

$result = mysqli_query($con, "select Id as user_id,Name as user_name from users");
$data = mysqli_fetch_all($result, MYSQLI_ASSOC);

echo json_encode($data);

?>