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

时间:2016-10-24 02:53:16

标签: php mysql

如何使用单独的php文件从MySQL数据库中检索一个值,并使用我从数据库中获取的值更改html(也是一个separete文件)控件? *我正在编程 例 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>

请提前帮助和感谢..&gt;&gt;&gt;

1 个答案:

答案 0 :(得分:0)

  1. 在你的html中使用ajax联系php。
  2. php会传回ajax json数据 一旦你从php收到json数据 - 很容易迭代它并将这些数据添加到html。
  3. 可能有用的几个链接 1. http://api.jquery.com/jquery.ajax/
    2. http://php.net/manual/en/function.json-encode.php

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