【发布时间】:2021-03-22 06:25:27
【问题描述】:
我正在尝试按名称搜索并在同一页面上显示结果。 我的 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>
当我搜索时,结果会显示在一个全新的页面上。但我希望结果显示在与所有现有页面布局、页眉和页脚相同的页面上。 我怎样才能做到这一点? 提前致谢!
【问题讨论】:
-
1- 对于布局,可以看到新页面,但包含
include('header.php")的布局 2- 如果您知道ajax是什么,请使用ajax在同一页面上查看结果 -
创建
function并从您的函数返回值。最后在php内的html页面中使用function。