我想在index.html中显示这些值
如何从php获取html值? display.php代码如下
var result = document.getElementById("get").innerHTML;
index.html代码如下
require "conn2.php";
$id = $_POST['s_id'];
echo $id;
$mysql_qry = "select * from Questions where Id='$id'";
$disp = mysqli_query($conn, $mysql_qry);
if(mysqli_num_rows($disp) > 0){
$result = mysqli_fetch_assoc($disp);
//echo"<br/> Question id: " .$result['Id'];
echo"<br/> Question: <br/>" .$result['Question'];
$mysql_qry = "select * from Answers where ForeignKey='$id'";
$disp2 = mysqli_query($conn, $mysql_qry);
while($result2 = mysqli_fetch_assoc($disp2)){
//echo"<br/> Answer id: " .$result2['Id'];
echo"<br/> " .$result2['Answer'];
//echo"<br/> Value: " .$result2['Value'];
}
}
答案 0 :(得分:4)
&#34;我想在index.html&#34;
中显示这些值
您需要指示您的服务器处理.html
个文件&#34;作为php&#34;如果您的服务器支持,请通过.htaccess
,或将.html
文件重命名为.php
,或使用ajax调用。
您还可以使用<iframe>
,其中来源为.php
文件。
您还需要在https://stackoverflow.com/revisions/43834696/5 --- https://i.stack.imgur.com/2Axrz.png看到一些屏幕截图后,通过localhost而不是file:///
在网络服务器上运行此功能。
您的代码也可以使用SQL注入;使用准备好的声明:
答案 1 :(得分:0)
如果您不希望页面刷新并且仅在index.html上运行,则可以使用ajax将结果输入index.html。
要执行此操作在index.html中包含jquery,然后您可以像这样从php文件中检索数据 在index.html
<html>
<head>
<title>Title of html</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<form id="displayform" method="POST">
<p>Enter the id of question</p><input type="text" name="s_id"><br/>
<input type="submit" name="submit" value="show me">
</form>
Result:
<div id="result">
</div>
</body>
</html>
<script>
$(function() {
$("#displayform").submit(function(e) {
//prevent Default functionality
e.preventDefault();
$.ajax({
url: 'display.php',
type: 'post',
dataType: 'html',
data: $("#displayform").serialize(),
success: function(response) {
$("#result").html(response);
}
});
});
});
</script>