Php 运行mysql查询onclick from index page
我在本地主机上有一个index.php,我想在这里放置几个按钮来运行不同的sql查询。查询将如下所示:Php 运行mysql查询onclick from index page,php,html,mysql,Php,Html,Mysql,我在本地主机上有一个index.php,我想在这里放置几个按钮来运行不同的sql查询。查询将如下所示: <table class="table1"> <tr> <th>Date</th> <th>Model</th> <th>Type</th> <th>InProduction</th> <th>Price</th> &
<table class="table1">
<tr>
<th>Date</th>
<th>Model</th>
<th>Type</th>
<th>InProduction</th>
<th>Price</th>
<th>Range</th>
<th>MaxSpeed</th>
<th>HP</th>
<th>Country</th>
<th>EType</th>
<th>Make</th>
<th>MPG</th>
<th>Seats</th>
</tr>
<?php
include ("config.php");
$sql = "SELECT Date, Model, Type, InProduction, Price, Range, MaxSpeed, HP, Country, EType, Make, MPG, Seats FROM Auto order by Date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$counter = 0;
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["Date"]."</td><td>" . $row["Model"] . "</td><td>"
. $row["Type"]. "</td><td>". $row["InProduction"]. "</td><td>". $row["Price"]. "</td><td>".$row["Range"]. "</td><td>". $row["MaxSpeed"].
"</td><td>". $row["HP"]."</td><td>". $row["Country"]."</td><td>". $row["Etype"]."</td><td>". $row["Make"]. "</td><td>". $row["MPG"].
"</td><td>". $row["Seats"]."</td></tr>";
$counter++;
if($counter % 33 == 0) { ?>
</table>
<table class="table1">
<tr>
<th>Date</th>
<th>Model</th>
<th>Type</th>
<th>InProduction</th>
<th>Price</th>
<th>Range</th>
<th>MaxSpeed</th>
<th>HP</th>
<th>Country</th>
<th>EType</th>
<th>Make</th>
<th>MPG</th>
<th>Seats</th>
</tr>
<?php }
}
echo "</table>";
} else { echo "0 results"; }
$conn->close();
?>
</table>
日期
模型
类型
生产中
价格
范围
最大速度
惠普
国家
词缀
制作
MPG
座位
日期
模型
类型
生产中
价格
范围
最大速度
惠普
国家
词缀
制作
MPG
座位
这里我包括了页面上显示的所有参数。相反,我想做的是在每个查询中使用不同的参数。因此,每次单击一个按钮,我都能看到索引页面上以表格形式列出的不同参数。因此,我计划创建几个php文件来列出不同的参数,并通过单击index.php上的按钮来运行它们。我如何使用onclick按钮来执行此操作?这是完成这项任务最合适的方式吗?注意:我将仅在本地发布此内容。谢谢。根据您的具体操作,您也可以使用查询字符串打开/关闭您希望用户查看的查询/数据。只需在onclick事件中设置参数,即可根据需要将查询字符串添加到按钮中的url:
按钮1
PHP中查询的切换如下所示:
if($_GET['show'] == "firstdata") {
//
// the first type query here
//
}elseif($_GET['show'] == "seconddata") {
//
// the second type query here
//
}
我找到了解决这个问题的ajax解决方案。它不是很整洁和短,但它的工作。我愿意以任何较短的方式完成这项任务,如果您能与我分享,我将不胜感激。谢谢以下是解决方案的来源: 因此,对于每个按钮,都需要使用相应的Id重复ajax代码 HTML:
问题1
阿贾克斯:
document.getElementById('actbutton1')。onclick=function()
{
var xhr=new XMLHttpRequest();
open(“GET”,“query1.php”,true);
xhr.onreadystatechange=函数()
{
如果(xhr.readyState==4&&xhr.status==200)
{
document.getElementById(“querydiv”).innerHTML=xhr.responseText;
}
}
xhr.send();
}
您需要使用AJAX向服务器发送请求,然后显示结果。如果没有AJAX,可以这样做吗?因为我将只在我的计算机/服务器上使用它。您可以在本地安装中使用ajax。您可以编写一个onclick
函数来填充表单字段,然后提交表单。您可以为每个页面创建单独的文件,并将每个页面的链接添加到其他页面。不过不漂亮。使用AJAX要好得多。你能提供一个例子的链接吗?请,我查一下。但我计划使用几个查询/按钮。对于出现故障的答案和评论,我感到抱歉,我对这一点不熟悉。。您还可以使用多个查询/按钮。如果您想让多个函数相互独立工作,您只需确保它们的值也在查询字符串中传递,例如当您单击另一个按钮时。
<div id="querydiv"> </div>
<button id='actbutton1'>query1</button>
<script>
document.getElementById('actbutton1').onclick=function()
{
var xhr = new XMLHttpRequest();
xhr.open("GET", "query1.php", true);
xhr.onreadystatechange = function ()
{
if (xhr.readyState==4 && xhr.status==200)
{
document.getElementById("querydiv").innerHTML=xhr.responseText;
}
}
xhr.send();
}
</script>