如何从 php 分配 html 值?

  • 本文关键字:html 分配 php php html
  • 更新时间 :
  • 英文 :


我想在索引中显示此值.html

如何从 php 获取 html 值? 显示.php代码如下

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'];
}
}

索引.html代码如下

<html>
<head>
<title>Title of html</title>
</head>
<body>
<form action="display.php" 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>
</body>

它的工作原理就像 显示.php 索引.html

"我想在索引中显示此值.html">

如果你的服务器支持,你需要指示你的服务器通过.htaccess.html文件"当作php",或者将你的.html文件重命名为.php,或者使用ajax调用。

您还可以使用源为.php文件的<iframe>

您还需要通过本地主机从网络服务器上运行它,而不是在 https://stackoverflow.com/revisions/43834696/5 --- https://i.stack.imgur.com/2Axrz.png 中看到一些屏幕截图后file:///

您的代码也可以接受SQL注入;使用预准备语句:

  • https://en.wikipedia.org/wiki/Prepared_statement

如果您不希望页面刷新并且只处理索引.html则可以将 ajax 的结果放入索引.html。

要做到这一点 在你的索引中.html包括 jquery,然后你可以像这样从 php 文件中检索数据。
在索引中.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>

最新更新