首页 > 解决方案 > 我希望 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>

当我搜索时,结果会显示在一个全新的页面上。但我希望结果显示在与所有现有页面布局、页眉和页脚相同的页面上。我怎样才能做到这一点?预先感谢!

标签: phphtmlmysqlsearch

解决方案


假设您的第一个文件名是 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>

推荐阅读