[英]How to return the mysql query result back to the html page after execution php script?
I am having a form where the user enters 2 variables. 我有一个表单,用户输入2个变量。 These 2 variables are used in my mysqlquery.
这两个变量用在我的mysqlquery中。 The result can be either: no matches or 1 or more matches.
结果可以是:无匹配或1个或多个匹配。 In each case I would like to have the output of that sql query as result on the original webpage below the entry fields (in the "queryresult" text field).
在每种情况下,我希望将sql查询的输出作为结果在输入字段下方的原始网页上(在“queryresult”文本字段中)。 How to do that?
怎么做?
The query is working but after clicking the button a new page is opened with the result of the query which is what I don't want. 查询正在运行,但在单击按钮后,将打开一个新页面,其中包含查询结果,这是我不想要的。
you can see the form here: www.larscichowski.nl/coinexchange 你可以在这里看到表格:www.larscichowski.nl/coinexchange
I tried already with hidden iframe and checked the answers on a similar question 我已经尝试使用隐藏的iframe并检查了类似问题的答案
within the html this is the code for the form part: 在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> </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>
the query.php file looks like this: 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();
?>
You can submit the form to the same page and have the php code in the same file. 您可以将表单提交到同一页面,并将php代码放在同一个文件中。
To do this, wrap your PHP code within if($_SERVER['REQUEST_METHOD'] == 'POST')
, so it will only run when the form is submitted. 为此,请将您的PHP代码包装在
if($_SERVER['REQUEST_METHOD'] == 'POST')
,这样它才会在提交表单时运行。
You need to change your form tag so it submits to the current page (or the page name in the action): 您需要更改表单标记,以便提交到当前页面(或操作中的页面名称):
<form method="post" action="" class="contact-form">
Then, you can change your result part to something like this. 然后,您可以将结果部分更改为此类型。 (So it will store the message that you want to display into a variable):
(因此它会将您要显示的消息存储到变量中):
$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";
}
Finally, you can echo the message anywhere in your page by doing this: 最后,您可以通过执行以下操作在页面中的任何位置回显消息:
if(isset($message)) {
echo $message;
}
Your page will look something like this: 您的页面看起来像这样:
if($_SERVER['REQUEST_METHOD'] == 'POST') {
// The contents of query.php
}
// the html code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.