HTML 下拉菜单不显示任何内容


Html Dropdown Menu Displays nothing

当我单击下拉列表中的值时,我正在尝试打开我的表。当我查看页面代码源时,表值在那里,但它没有显示。我想使用下拉列表中的值从 mysql 查询,并使用来自 mysql 查询的数据填充表。

这是我的代码:

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet"href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script type="text/javascript">
  function jsFunction(value){   
    <?php
       $con=mysqli_connect("localhost","root","","dengue");
       // Check connection
       if (mysqli_connect_errno())
       {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
       }
       $result = mysqli_query($con,"SELECT * FROM mapping_details where case_type='value'";
      echo '<div class="table-responsive ">          
      <table class="table table-condensed table-hover table-bordered">
      <thead>
  <tr>
    <th>Barangay</th>
    <th>Case Type</th>
    <th>Total Dengue Cases</th>      
  </tr>
</thead>
<tbody>';
  while($row = mysqli_fetch_array($result))
  {
   echo "<tr>";
   echo "<td>" . $row['brgy'] . "</td>";
   echo "<td>" . $row['case_type'] . "</td>";
   echo "<td>" . $row['total_case_brgy'] . "</td>";
   echo "</tr>";
  }
 echo '</tbody></table></div>';
 mysqli_close($con);
?>
}
 </script>
    </head>
    <body>
      <select id ="ddl" name="ddl" onchange="jsFunction(this.value);">
      <option value='1'>One</option>
      <option value='2'>Two</option>
      <option value='3'>Three</option>
    </select>
    </body>
    </html>

最初,在我的其他网页中,php代码在正文中。但是这一次,如果我使用下拉菜单,我无法使其工作,这就是为什么我将其放在函数中以便能够使用 onchange 函数的原因。我已经阅读了一些使用AJAX或JSON的答案,但我不熟悉这些答案,所以如果可能的话,我只想使用javascript,因为这可能是最简单的方法。

你在这里要做的是在浏览器中运行PHP代码,这是你做不到的。所有的HTML,CSS和JavaScript都被发送到客户端(你的浏览器),然后由浏览器渲染和初始化。当你调用你的onchange事件来调用jsFunction()时,你试图在该函数中执行PHP,但是你在客户端运行在chrome或firefox或某些浏览器中,你不能在那里执行PHP。PHP 必须在页面加载时运行,然后可以在发送到客户端之前更改 html、css 或 JavaScript。

有两种方法可以做你想做的事情。您可以发送Ajax请求(使用javascript调用您的服务器)以获取新数据,或者您可以提交重新加载页面的表单,您可以像您喜欢的那样运行PHP - 在返回给客户端(浏览器)之前修改html,css和/或javascript。

这是非 ajax 方式,它只是不断重新加载页面

<!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet"href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <?php
        $value = isset( $_POST['ddl'] ) ? $_POST['dll'] : 1; // we are setting 1 as the default
       $con=mysqli_connect("localhost","root","","dengue");
       // Check connection
       if (mysqli_connect_errno())
       {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
       }
       $result = mysqli_query($con,"SELECT * FROM mapping_details where case_type='{$value}'";
      $html= '<div class="table-responsive ">          
      <table class="table table-condensed table-hover table-bordered">
      <thead>
  <tr>
    <th>Barangay</th>
    <th>Case Type</th>
    <th>Total Dengue Cases</th>      
  </tr>
</thead>
<tbody>';
  while($row = mysqli_fetch_array($result))
  {
   $html.= "<tr>";
   $html.= "<td>" . $row['brgy'] . "</td>";
   $html.= "<td>" . $row['case_type'] . "</td>";
   $html.= "<td>" . $row['total_case_brgy'] . "</td>";
   $html.= "</tr>";
  }
 $html.= '</tbody></table></div>';
 mysqli_close($con);
?>
}
 </script>
    </head>
    <body>
      <form method="POST" action="">
        <select id="ddl" name="ddl">
          <option value='1'>One</option>
          <option value='2'>Two</option>
          <option value='3'>Three</option>
        </select>
      </form>
      <script>
        $("select#ddl").on("change",function(){
          $('form').submit();
        });
      </script>
      <?php echo $html; ?>
    </body>
    </html>