【问题标题】:I want the php results to be displayed on the same page instead of a completely new page我希望 php 结果显示在同一页面上,而不是一个全新的页面
【发布时间】: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

标签: php html mysql search


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2014-03-10
    • 1970-01-01
    • 2015-11-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2020-05-30
    • 2021-09-20
    • 1970-01-01
    相关资源
    最近更新 更多