2016-03-04 70 views
2

我想diplay我的表,当我点击下拉列表中值。当我查看页面代码源时,表值存在但未显示。我想从mysql中使用下拉列表中的值进行查询,并使用mysql查询中的数据填充表。的Html下拉菜单显示什么

这里是我的代码:

<!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,因为它可能是最简单的方法。

+0

你的PHP代码使用回声'script'标签里面......我想这应该是*不*内部'脚本“标签并移动到”body“。 – Mottie

+0

嗨@Mottie。谢谢你指出我的错误。 Jon Foley的回答下面会产生错误并且什么都不会查询。你能告诉我如何以正确的方式执行此操作吗? – CretienSC

+0

对不起,我不知道PHP足以帮助;但它听起来像查询代码需要重新检查。 – Mottie

回答

0

你正在尝试做的,是在浏览器中,你不能做运行PHP代码。所有的HTML,CSS和JavaScript都发送到客户端(您的浏览器),然后由浏览器呈现并初始化。当你打电话给你onchange事件调用jsFunction()你正在尝试在该函数内执行PHP但是 - 你是在Chrome或Firefox或一些浏览器中运行的客户端,你不能执行PHP那里。 PHP必须在页面加载时运行,然后可以在发送到客户端之前更改html,css或JavaScript。

有做你想做的事情有两种方式。您可以发送一个Ajax请求(调用你的服务器使用JavaScript)来获取新的数据,也可以前提交重新加载页面的表单和你一样是舒适与 - 修改HTML,CSS可以运行PHP,和/或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> 
+0

我得到这个错误: '通知:未定义指数:DLL在C:\ XAMPP \ htdocs中\ Newfolder \ testing.php第10行'。第二件事是,它什么也没有返回,我只能看到表头,但没有任何价值,所以也许它什么都不问。谢谢。 – CretienSC

+0

我已解决问题。现在代码是测试代码。它的运行良好。 –

+0

现在代码更有意义。希望它能帮助你。 –