如何在执行php脚本后将mysql查询结果返回到html页面?

问题描述 投票:0回答:1

我有一个表单,用户输入2个变量。这两个变量用在我的mysqlquery中。结果可以是:无匹配或1个或多个匹配。在每种情况下,我希望将sql查询的输出作为结果在输入字段下方的原始网页上(在“queryresult”文本字段中)。怎么做?

查询正在运行,但在单击按钮后,将打开一个新页面,其中包含查询结果,这是我不想要的。

你可以在这里看到表格:www.larscichowski.nl/coinexchange

我已经尝试使用隐藏的iframe并检查了类似问题的答案

在html中,这是表单部分的代码:

        <section class="section-form" id="form">
            <div class="row" >
                <h2>Coin Exchange Finder</h2>
            </div>
            <div class="row">
            <form method="get" action="query.php" class="contact-form">



                    <div class="row">


                        <div class="col span-1-of-3">
                            <label for="name">Source Coin</label>
                        </div>

                        <div class="col span-1-of-3">
                            <input class="typeahead form-control" name="sourcecoin" id="sourcecoin" type="text" required>
                        </div>



                    </div>
                    <div class="row">


                        <div class="col span-1-of-3">
                            <label for="name">Destination Coin</label>
                        </div>

                        <div class="col span-1-of-3">
                            <input class="typeahead form-control" name="destcoin" id="destcoin" type="text" >
                        </div>
                    </div>
                    <script type="text/javascript">



                    <div class="row">
                        <div class="col span-1-of-3">
                            <label>&nbsp;</label>
                        </div>

                        <div class="col span-2-of-3">
                            <input type="submit" value="Find matching 
 exchanges">
                        </div>
                    </div>

                    <div class="row">
                        <div class="col span-1-of-3">
                            <label>We found the following matches:</label>
                        </div>
                        <div class="col span-2-of-3">
                            <input type="text" id="queryResult"/>
                        </div>
                    </div>
                </form>
            </div>
        </section>

query.php文件如下所示:

<?php
$servername = "xx";
$username = "xx";
$password = "xx";
$dbname = "xx";
$sourcecoin = strip_tags(trim($_POST["sourcecoin"]));
$destcoin = strip_tags(trim($_POST["destcoin"]));


// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    echo "Connection not established. Check credentials";
} 


$sql = "SELECT Pairs_Source.Exchange, Exchanges.HyperLink
FROM Pairs AS Pairs_Source INNER JOIN Pairs AS Pairs_Dest ON 
Pairs_Source.Exchange = Pairs_Dest.Exchange
Left join Exchanges on Pairs_Source.Exchange=Exchanges.Exchange
WHERE Pairs_Source.Coin='$sourcecoin' AND Pairs_Dest.Coin='$destcoin'";


$result = $conn->query($sql);



$json = [];
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $json[]=$row['Exchange'];
        //echo "<br> They have got the following exchange(s) in common: ". 
 $row["Exchange"] ."<br>";
    }

} else {
        echo "Unfortunately these 2 coins don't have an exchange in 
 common";
}
    echo json_encode($json);

$conn->close();
?>
php html mysql
1个回答
0
投票

您可以将表单提交到同一页面,并将php代码放在同一个文件中。

为此,请将您的PHP代码包装在if($_SERVER['REQUEST_METHOD'] == 'POST')中,这样它才会在提交表单时运行。


您需要更改表单标记,以便提交到当前页面(或操作中的页面名称):

<form method="post" action="" class="contact-form">

然后,您可以将结果部分更改为此类型。 (因此它会将您要显示的消息存储到变量中):

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $message = "<br> They have got the following exchange(s) in common: 
 ". $row["Exchange"] ."<br>";
     }

 } else {
         $message = "Unfortunately these 2 coins don't have an exchange in 
 common";
 }

最后,您可以通过执行以下操作在页面中的任何位置回显消息:

if(isset($message)) {
    echo $message;
}

您的页面看起来像这样:

if($_SERVER['REQUEST_METHOD'] == 'POST') {

  // The contents of query.php

}

// the html code
© www.soinside.com 2019 - 2024. All rights reserved.