【问题标题】:How to display PHP on the same html page如何在同一个html页面上显示PHP
【发布时间】:2018-01-10 16:08:02
【问题描述】:

我有一个 HTML 表单,我可以在其中使用 PHP 搜索 MySQL 数据库。运行 PHP 代码后,我在另一个页面上获得了搜索结果。我想在表单旁边的表格中获取搜索结果。代码如下:

HTML:

 <form name="select"action="select.php" method="post">
 Name:
 <input type="submit" name="sub">
 </form>

PHP:

$name = $_POST['name'];
$sql = "SELECT * FROM db where name ='$name'";
if($result = mysqli_query($conn, $sql)){
    if(mysqli_num_rows($result) > 0){ 
        echo "<table>"; //tabellen
        echo "<table border=1";
            echo "<tr>";
                echo "<th>Email</th>";
                echo "<th>Name</th>";
                echo "<th>nbr</th>";
            echo "</tr>";
        while($row = mysqli_fetch_array($result)){ 
            echo "<tr>";
                echo "<td>" . $row['email'] . "</td>";
                echo "<td>" . $row['name'] . "</td>";
                echo "<td>" . $row['nbr'] . "</td>";

            echo "</tr>";
        }
        echo "</table>";
        mysqli_free_result($result);

【问题讨论】:

  • 使用ajax发送请求并在收到响应时操作dom或将表单操作设置为同一页面并将PHP代码放在同一页面中
  • 尝试使用 AJAX 调用或 jquery 的 $.load()
  • 这些链接应该可以帮助到w3schools.com/xml/ajax_php.asp || api.jquery.com/jquery.ajax
  • 您的 html 无效 - 您有 &lt;table&gt;&lt;table border=1 ~ 未关闭且嵌套不正确
  • 要么 ajax 要么将整个代码放在同一个文件/页面中,并将操作设置为 self。

标签: php html mysql mysqli


【解决方案1】:

最简单的选择是在同一页面上处理搜索请求的 php 代码并删除表单操作。

也就是说,由于 sql 语句中的嵌入变量,您的代码容易受到 SQL 注入的影响。当您使用mysqli 时,您应该可以轻松地将其翻译为使用prepared statements,例如:

例子:

if( $_SERVER['REQUEST_METHOD']=='POST' && !empty( $_POST['name'] ) ) {

    $name = $_POST['name'];
    /*
        Better to select specific fields rather than all fields, 
        then bind those to specific result variables.
    */
    $sql='select `email`,`name`,`nbr` from `db` where `name`=?';
    $stmt=$con->prepare( $sql );

    if( $stmt ){

        $stmt->bind_param( 's', $name );
        $result=$stmt->execute();

        if( $result && $stmt->num_rows > 0 ){

            $stmt->store_result();
            $stmt->bind_result( $email, $name, $nbr );

            echo '<table>
                    <tr>
                        <th>Email</th>
                        <th>Name</th>
                        <th>nbr</th>
                    </tr>';

            while( $stmt->fetch() ){
                printf('
                    <tr>
                        <td>%s</td>
                        <td>%s</td>
                        <td>%s</td>
                    </tr>', $email, $name, $nbr );
            }

            echo '</table>';
        }
    }
}

用 ajax 来做,然后可能会沿着这些思路(select.php 有上述代码或类似代码)

document.querySelector('sub').onclick=function(event){
    event.preventDefault()
    var _callback=function(r){
        document.querySelector('form[name="select"]').insertAdjacentHTML('afterend',r);
    }

    var xhr=new XMLHttpRequest();
    xhr.onreadystatechange=function(){
        if( this.status==200 && this.readyState==4 )_callback.call( this, this.response );
    }
    xhr.open( 'POST', 'select.php', true );
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send( 'name='+document.querySelector('name').value );
}

【讨论】:

    【解决方案2】:
    search.php
    
    <form name="select" action="" method="post">
     Name:
     <input type="submit" name="sub">
    </form>
    
    <?php
    
    if(isset($_POST['sub'])){
    $name = $_POST['name'];
    $sql = "SELECT * FROM db where name ='$name'";
    if($result = mysqli_query($conn, $sql)){
        if(mysqli_num_rows($result) > 0){ 
            echo "<table>";
            echo "<table border=1">;
            echo "<tr>";
            echo "<th>Email</th>";
            echo "<th>Name</th>";
            echo "<th>nbr</th>";
            echo "</tr>";
            while($row = mysqli_fetch_array($result)){ 
                echo "<tr>";
                    echo "<td>" . $row['email'] . "</td>";
                    echo "<td>" . $row['name'] . "</td>";
                    echo "<td>" . $row['nbr'] . "</td>";
    
                echo "</tr>";
            }
            echo "</table>";
            mysqli_free_result($result);
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 2020-11-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多