在PHP中更改HTML内容

时间:2013-11-26 06:52:50

标签: php

我是PHP的新手,我在更改resultDiv内容

时遇到问题
if(mysql_num_rows($res)==0)
    {
           echo "<script>document.getElementById('resultDiv').innerHTML='No such flight';</script>";
          // echo "<script>alert('No Such filght');</script>";
           echo "No such flight";
    }
else{
    echo "<table border='1'><th>flight#</th><th>Dep. City</th><th>Arrival City</th><th>Dep. Date</th><th>Arrival Date</th><th>Total Seats</th>";
    while($row=mysql_fetch_array($res))
     echo "<tr><td>".$row['id']."</td><td>".$row['depCity']."</td><td>".$row['arrivCity']."</td><td>".$row['depDate'].
     "</td><td>".$row['arrivDate']."</td><td>".$row['totalSeats']."</td></tr>";
     echo "</table>";
    }
</div>
<div id="resultDiv">
Result Div
</div>

3 个答案:

答案 0 :(得分:1)

请避免使用mysql_*函数,因为它在最新版本中已弃用。您可以使用mysqliPDO

您可以显示如下信息: -

<?php
    $message = (mysql_num_rows($res) == 0) ? 'No Such Flight' : '';
?>

<div id="resultDiv">
 <?php echo $message;?>
</div>

答案 1 :(得分:0)

你可以这样做:

<div id="resultDiv">
<?php
   if (mysql_num_rows($res) == 0) {
       echo "No such flight";
   } else {
       echo "Flight found";
   }
?>
</div>

答案 2 :(得分:0)

你在id中获取拼写错误“resultDiv”,并​​在if条件之前定义div。

<div id="resultDiv">
Result Div
</div>



 if(mysql_num_rows($res)==0)
 {
       echo "<script>document.getElementById('resultDiv').innerHTML='No such flight';</script>";
       echo "No such flight";
 }