我试图通过jquery Ajax查询数据库。我想选择一个选项并加载一个表来返回结果。
我的页面拨打电话如下:
<!doctype html>
<head>
<script src="jquery/jquery-ui-1.11.2/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="jquery/jquery-ui-1.11.2/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="jqueryform/dist/jquery.validate.js"></script>
<script src="jqueryform/dist/jquery.validate.min.js"></script>
<script>
$(document).ready(function() {
$('.btnSearch').click(function(){
});
$('form').submit(function(e){
});
function makeAjaxRequest() {
$.ajax({
url: 'addithandle1.php',
type: 'get',
data: {name: $('input#batchtype').val()},
success: function(response) {
$('table#resultTable tbody').html(response);
}
});
}
</script>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="">IgG Dashboard</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="Login_success.php">New Handover</a></li>
<li><a href="oldfiles.php">Old Handovers</a></li>
<li><a href="dgam.php">Formulations</a></li>
<li><a href="current.php">Current Processing</a></li>
<li><a href="midanamenu.php">Analysis</a></li>
<li><a href="oldfiles.php">Other</a></li>
<li id= "dash"><p >Welcome <?php echo " "
.$_SESSION['myuser']." ";?><a class="btn bg-info" href="adm.php">Admin Panel</a></p></li>
</ul>
</div><!-- /.nav-collapse -->
</div><!-- /.container -->
</div><!-- /.navbar -->
<br><br><br><br><br>
<div class="container-fluid">
<div class="row-fluid">
<div class="col-md-3">
<?php
include "config.php";
// Check connection
if (mysqli_connect_errno($con))
{
}
else
{
$result = mysqli_query($con, "SELECT * FROM script ");
echo " <Form method='get'> <label>Script :</label> <select name='batchtype' >";
}
while($row = mysqli_fetch_array($result))
{
echo "<option value = '".$row['scriptname']."'>".$row['scriptname']."</option>";
}
echo "</select>";
echo "<button class='btn-search'>Load Script </button></form>";
?>
<table id="resultTable">
<thead>
<tr>
<th>scriptname</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
因此,当此页面加载时,进行PHP调用以生成表单AND PARTS。必须采用这种方式,因为选项在站点的后端输入并回显到前端。
然后我尝试选择一个选项点击提交并从我的数据库中提取一行并返回到我的#resultstable。我的PHP文件如下。
<?php
include 'config.php';
$batchtype2 = ($_GET['batchtype']);
$batchtype2 = mysqli_real_escape_string($con,$batchtype2);
$sql = "SELECT * FROM script WHERE scriptname = '".$batchtype2."' ";
$result = mysqli_query($con,$sql);
$count=mysqli_num_rows($result);
if($count==0 ){
echo "</br></br></br></br></br></br></br><p> No Matching results found</p>";
}
else{
while($row = mysqli_fetch_array($result)) {
echo '<td>'.$row['scriptname'].'</td>';
}
}
mysqli_close($con);
?>
该函数正在更改网址并发出一些请求,但我没有在表格中获得所需的结果(没有数据)
我已经尝试调整教程中的代码,我可以找到一些将表格输出回页面的代码。
任何帮助将不胜感激。我可以用PHP来处理所有这些,但是想让我的页面更具动态性,所以我试图探索AJAX
另外值得补充一点,php脚本在没有ajax的情况下工作。
答案 0 :(得分:0)
替换tbody元素的内容时,您的响应应包含<tr>
- 元素