Ajax显示动态数据php在html下拉

Ajax show Dynamic data of php in html drop down

本文关键字:html 下拉 php 数据 显示 动态 Ajax      更新时间:2023-09-26

我正试图从数据库中获取数据,并在下面的代码中填充数据。

我无法填充它们,因为数据输出说:来自PHP的"undefined index: selected "可能是数据没有从ajax传递或PHP无法读取。

HTML:

<select id="dynamicDropdown"></select>

JS:

var storedValue = localStorage.getItem("id");
    $.ajax({
        url : "DD.php",
        data: {"storedValue":storedValue},

        success : function(data){
            alert(data);
            console.log(data)  // data should be an array that you have mentioned.
            data.map(function(c){
                $("#dynamicDropdown").append("<option value="+c.C_CO+">"+c.C_NAME1+"</option>");
            });
        }
    });
    };
PHP:

$mysqli=mysqli_connect('xxx','xxx','xxx','xxx');
$selectid =  mysqli_real_escape_string($mysqli,trim($_POST['storedValue']));

$query112 ="SELECT * FROM dd_table  WHERE  id='$selectid'";
$result112 = mysqli_query($mysqli,$query112)or die(mysqli_error());
$num_row112 = mysqli_num_rows($result112);
while($row=mysqli_fetch_array($result112))
{

$response = array($row['dd_data'] );
echo json_encode($response); 
}

我可能错了,但你不应该这样写吗?$query112 ="SELECT * FROM dd_table WHERE id=' ' ' .$ SELECT . ' ' ';div ?

你不需要任何jQuery:

试试这个:

<select>
    <?php 
        foreach(mysqli_fetch_array($result112) as $x){
           echo "<option>". $x['dd_data']. "</option>";
        }
    ?>
</select>