Arshak Anjum Arshak Anjum - 1 year ago 54
HTML Question

How to dynamically change content in HTML based on php database values?

I have this web based app with PHP (mysql) backend. I have a page to enter the grades and position received in a particular event. I need the page to show the checkboxes (for the grades) only if they have not been entered before. So I have this column P1 in my database and if this is NULL i need to show the checkboxes.

This is what i have on the php side

<?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)

?>


and what i need to be displayed in P1 is NULL is as follows

<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>


How can i do this without putting echo in each and every HTML line.

Answer Source

You can try this. If you don't want to use echo, just enclose the html in the if without including the html in the php area

<?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 }?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download