php - 我希望 php 结果显示在同一页面上,而不是一个全新的页面
问题描述
我正在尝试按名称搜索并在同一页面上显示结果。我的 php 代码和 html 代码位于 2 个单独的文件中。下面给出的是我的 search.php 代码。
<?php
include_once '../connection.php';
include_once '../session.php';
$output = '';
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
if($count == 0) {
$output = 'There was no search result!';
}else{
while($row = mysqli_fetch_array($result)) {
$ID = $row['id'];
$Name= $row['customername'];
$Type = $row['type'];
$Phone = $row['phoneno'];
$Email = $row['email'];
$output .= '<div>' .$Name.' '.$Type.' '.$Phone.' '.$Email.'<div>';
}
}
}
?>
<?php print("$output");?>
下面给出的是我的表单的 html 代码。
<form action="search.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
当我搜索时,结果会显示在一个全新的页面上。但我希望结果显示在与所有现有页面布局、页眉和页脚相同的页面上。我怎样才能做到这一点?预先感谢!
解决方案
假设您的第一个文件名是 index.php。然后需要在表单操作中调用这个文件并稍微重构一个文件。现在它有 $output 数组,它是空的。如果它包含搜索参数,它会将数据填充到 $output 数组,然后在 html 部分检查 $output 数组是否有数据来显示它。
<?php
include_once '../connection.php';
include_once '../session.php';
$output = array();
if(isset($_POST['search'])){
$searchquery=$_POST['search'];
$result = mysqli_query($conn,"SELECT * FROM details where customername LIKE '%$searchquery'");
$count = mysqli_num_rows($result);
while($row = mysqli_fetch_array($result)) {
$output[] = $row;
}
}
?>
<!DOCTYPE html>
<html>
<body>
<form action="index.php" method="POST" >
<center><input type="text" name="search" placeholder="Search by name" >
<input type="submit" value="Search"></center>
<br>
<?php if(count($output) > 0): ?>
<table id="myTable">
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Telephone Number</th>
<th>Email</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php foreach($output as $row): ?>
<tr>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["customername"]; ?></td>
<td><?php echo $row["type"]; ?></td>
<td><?php echo $row["phoneno"]; ?></td>
<td><?php echo $row["email"]; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</form>
</body>
</html>
推荐阅读
- ios - 有什么方法可以在 Swift 中创建自定义下拉菜单?
- javascript - 隐藏/显示数据表中的列
- javascript - Nuxt:身份验证 + Axios 设置
- python - Python 3 从现有列表中随机选择
- c++ - 为什么我不能用 std::tuple 保证复制省略?
- windows - Docker暴露端口不可达
- javascript - Gulp 创建一个新的 dist 文件夹
- css - 在反应中渲染wordpress模板时的样式问题
- mysql - 如何使用存储在另一个数据库中的权限过滤数据库结果
- javascript - 以迭代方式更改 JSON 对象的每个关键元素