gpt4 book ai didi

php - 如何使用单独的 php 文件从 MySQL 数据库检索一个值,并使用从数据库获取的值更改 html 控件?

转载 作者:行者123 更新时间:2023-11-29 20:09:38 26 4
gpt4 key购买 nike

如何使用单独的 php 文件从 MySQL 数据库检索一个值,并使用从数据库获取的值更改 html(也是一个单独的文件)控件?* 我是编程初学者例子html页面

    <div class="container" style="padding:60px">
<div class="row">
<div class="col-md-1=12 col-xs-12">

<form method="post" action="SmartSwitch.php" >
<h1 align="center">SmartHome</h1>
<br> <hr>
<div align="center">
<B>Switches</B>
<P>Switch 1 - Level 1 , Room 1</P>

<p><b>Light :</b><br>

<!--I want to change this control for example <p>status</p> into ON-->
> <p><b>status :</b>

<input class="btn btn-primary btn-block" type="submit" value=" OFF " name="submit1_L1" style="font-weight:bold;border:0px sloid white;border-radius:10px;" >
</p>
<p>
<input onclick="ChangeText()" class="btn btn-primary btn-block" type="submit" value=" ON " name="submit1_L2" style="font-weight:bold;border:0px sloid white;border-radius:10px;" >
</p>

<br>
<p><b>Air Condition :</b><br><input class="btn btn-primary btn-block" type="submit" value=" OFF " name="submit1_C1" style="font-weight:bold;border:0px sloid white;border-radius:10px;">
</p>

<p>
<input class="btn btn-primary btn-block" type="submit" value=" ON " name="submit1_C2" style="font-weight:bold;border:0px sloid white;border-radius:10px;">
</p>

<br><span style="color:#F00"><hr></span>
<P>Switch 2 - Level 2 , Living Area</P>
<p><b>Light :</b><br><input class="btn btn-primary btn-block" type="submit" value=" OFF " name="submit2_L1" style="font-weight:bold;border:0px sloid white;border-radius:10px;">
</p>

<p>
<input class="btn btn-primary btn-block" type="submit" value=" ON " name="submit2_L2" style="font-weight:bold;border:0px sloid white;border-radius:10px;">
</p>



<br>
<p><b>Air Condition :</b><br><input class="btn btn-primary btn-block " type="submit" value=" OFF " name="submit2_C1" style="font-weight:bold;border:0px sloid white;border-radius:10px;">
</p>

<p>
<input class="btn btn-primary btn-block" type="submit" value=" ON " name="submit2_C2" style="font-weight:bold;border:0px sloid white;border-radius:10px;">
</p>



<br><br><hr>
<input class="btn btn-primary" type="submit" value="خـــــــــــــــروج" name="submit_Exit" style="font-weight:bold">
</div>


</form>

</div>
</div>
</div>

这是 php 代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SmartSwitch_P</title>
</head>

<body>
<?php


$con=mysqli_connect('localhost','root','root');

if(!$con)
{
echo "Not connected";
}

if (!mysqli_select_db($con,'smarthome'))
{
echo "Database is not selected";
}
//============== my select statement======================================
$sqlSELECT = "SELECT switch_status FROM smartswitch WHERE switch_no=1 AND device='Light' limit 1";** $result = mysqli_query($conn, $sqlSELECT); if (mysqli_num_rows($result) > 0) { { echo "Light 1 status is : " . $row["switch_status"]; } } else { echo "0 results"; } mysqli_close($conn);


?>

</body>
</html>

请提前提供帮助并致谢..>>>

最佳答案

  1. 在 html 中使用 ajax 来联系 php。
  2. php 会传回 ajax json 数据。
    一旦您从 php 接收到 json 数据,就可以很容易地遍历它并将这些数据添加到 html 中。

几个可能有帮助的链接
1.http://api.jquery.com/jquery.ajax/
2.http://php.net/manual/en/function.json-encode.php

所以基本上你不需要改变html文件。您将根据从 php 收到的输出动态生成元素。

关于php - 如何使用单独的 php 文件从 MySQL 数据库检索一个值,并使用从数据库获取的值更改 html 控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40210068/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com