Good morning, I must insert values in a select in a html file from a database, I can not modify the extention of the file html. I write this with php and it work, but i must insert this functionality in a existing html file, how i can do that?
<?php
$user = "root";
$pass = "root";
$db = "dbname";
$connect = new mysqli('localhost', $user, $pass, $db) or die("You Fail");
$query ="SELECT * FROM `tableName`";
$result = mysqli_query($connect,$query);?>
<HTML>
<HEAD>
</HEAD>
<BODY>
<select>
<?php while($row1 = mysqli_fetch_array($result)):;?>
<option value="<?php echo $row1[0];?>"><?php echo $row1[1];?></option>
<?php endwhile;?>
</select>
<select>
<option value="<?php echo $row1[0];?>">
<?php echo $row1[0];?>
</option>
</select>
</BODY>
</HTML>
Create a .htaccess file in directory and add this code to .htaccess file
AddType application/x-httpd-php .html .htm
OR
AddHandler x-httpd-php .html .htm
我解决了导入jQuery的问题,并制作了一个脚本,该脚本从php文件中获取json格式的数据,并且该脚本将json转换为html格式,并将其追加到html文件中的正确位置
Create a php file getdata.php
<?php
$user = "root";
$pass = "root";
$db = "dbname";
$connect = new mysqli('localhost', $user, $pass, $db) or die("You Fail");
$query ="SELECT * FROM `tableName`";
$result = mysqli_query($connect,$query);
while($row1 = mysqli_fetch_array($result)){
print_r($b_option[]="<option value='".$row1[0]."'>".$row1[1]."</option>");
} ?>
after that give id to your select box add ajax code in your html file
<html>
<body>
<select id="select_data">
</select>
</body>
<script src="js/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
url:"getdata.php",
type:"post",
success:function(data){
$("#select_data").html(data);
}
});
});
</script>
</html>
please make sure that you added jquery.min.js file before ajax code
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.