如何基于php数据库值动态更改HTML中的内容?

时间:2017-05-23 09:05:09

标签: php html mysql web

我有这个基于Web的应用程序与PHP(mysql)后端。我有一个页面输入在特定事件中收到的成绩和职位。只有在之前没有输入时,我才需要页面显示复选框(等级)。所以我在我的数据库中有这个列P1,如果这是NULL,我需要显示复选框。

这就是我在php端的内容

<h2>Slot 1</h2>
<h3>Enter Result</h3>
<div id="c1" class="radio">
  <label><input type="radio" name="opt1radio" value=30>First</label>
  <label><input type="radio" name="opt1radio" value=20>Second</label>
  <label><input type="radio" name="opt1radio" value=10>Third</label>
</div>
<h3>Enter Grade</h3>
<div id="c2" class="radio">
  <label><input type="radio" name="opt2radio" value=5>A</label>
  <label><input type="radio" name="opt2radio" value=4>B</label>
  <label><input type="radio" name="opt2radio" value=3>C</label>
  <label><input type="radio" name="opt2radio" value=2>D</label>
  <label><input type="radio" name="opt2radio" value=0>DQ</label>
</div>
<div id="submit">
  <button onclick="myFunction()" type="button" class="btn btn-success">Submit</button>
</div>

我需要在P1中显示的是NULL如下

{{1}}

如果不在每个HTML行中放置回声,我该怎么做呢。

2 个答案:

答案 0 :(得分:0)

你可以试试这个。如果您不想使用echo,请将html括在if中,而不要将html包含在php区域

<?php
 $PID = (int)$_GET['PID'];
    $con = new mysqli("localhost","my_user","my_password",'events');
    $result = $con->query("SELECT * FROM eventlist WHERE Eid=$PID");
    global $output;
    $row =  $result->fetch_assoc();
    echo '<h1>'.$row["Ename"].'</h1>';
    if($row["P1"]==NULL){
  ?>

<h2>Slot 1</h2>
    <h3>Enter Result</h3>
    <div id="c1" class="radio">
      <label><input type="radio" name="opt1radio" value=30>First</label>

      <label><input type="radio" name="opt1radio" value=20>Second</label>

      <label><input type="radio" name="opt1radio" value=10>Third</label>
    </div>

   <h3>Enter Grade</h3>  
   <div id="c2" class="radio">
      <label><input type="radio" name="opt2radio" value=5>A</label>

      <label><input type="radio" name="opt2radio" value=4>B</label>

      <label><input type="radio" name="opt2radio" value=3>C</label>

      <label><input type="radio" name="opt2radio" value=2>D</label>

       <label><input type="radio" name="opt2radio" value=0>DQ</label>
    </div>
    <div id="submit">

      <button onclick="myFunction()" type="button" class="btn btn-success">Submit</button>
    </div>
<?php }?>

答案 1 :(得分:0)

我认为您唯一认为缺少的是逐个检索所有行,您可以使用:while ($row = $result->fetch_assoc()) {执行此操作,然后将所有代码放在:

<?php
 $PID = (int)$_GET['PID'];
    $con = new mysqli("localhost","my_user","my_password",'events');
    $result = $con->query("SELECT * FROM eventlist WHERE Eid=$PID");
    global $output;
    while ($row = $result->fetch_assoc()) {
    echo '<h1>'.$row["Ename"].'</h1>';
    if($row["P1"]==NULL){
  ?>

<h2>Slot 1</h2>
    <h3>Enter Result</h3>
    <div id="c1" class="radio">
      <label><input type="radio" name="opt1radio" value=30>First</label>

      <label><input type="radio" name="opt1radio" value=20>Second</label>

      <label><input type="radio" name="opt1radio" value=10>Third</label>
    </div>

   <h3>Enter Grade</h3>  
   <div id="c2" class="radio">
      <label><input type="radio" name="opt2radio" value=5>A</label>

      <label><input type="radio" name="opt2radio" value=4>B</label>

      <label><input type="radio" name="opt2radio" value=3>C</label>

      <label><input type="radio" name="opt2radio" value=2>D</label>

       <label><input type="radio" name="opt2radio" value=0>DQ</label>
    </div>
    <div id="submit">

      <button onclick="myFunction()" type="button" class="btn btn-success">Submit</button>
    </div>
<?php } }?>

一会儿,你的代码只显示第一个结果。

希望它有所帮助!