繁体   English   中英

从期权中获取价值

[英]Getting the value from 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>

我的问题是我是否从下拉列表中选择内容是否存在但没有价值。 请帮助。

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);
        }
 });
data: {instructor:$('#SELECT_ELEMTN_ID').val()},

根据范围和内容,您可能不想使用“ this”。

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);

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM