我有一个搜索字段的页面。该页面旨在将结果显示在同一页面上,当单击提交链接时,结果将显示在下方。提交按钮是一个链接而不是传统的提交按钮。我的问题是我已经完成了所有的PHP的东西,但当我点击链接,让结果出现,PHP没有运行。请有人帮帮我。下面是代码。感谢
<form action="" method="post">
<div class="col-md-9">
<div class="row">
<div class="col-md-7 no-padding">
<div class="inner">
<h3>Track & Trace</h3>
<span>Already have a load ID, please insert it below</span>
</div>
</div>
<div class="col-md-5 no-padding">
<input class="input-fullwidth" name="track" id="track">
</div>
</div>
</div>
<div class="col-md-3">
<a id="track-it" href="#section-tracking-result" class="btn-custom btn-fullwidth">Track it</a>
</div>
</form>
<?php
define('DB_HOST','localhost');
define('DB_USER','username');
define('DB_PASS','password');
define('DB_NAME','dbname');
$conn = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die(mysql_error());
mysql_select_db(dbname) or die(mysql_error());
$output = '';
if(isset($_POST['track'])) {
if(empty($_POST['track'])) {
echo "Tracking Code is Required";
}
$searchq = $_POST['track'];
$searchq = preg_replace("#[^0-9a-z]#i", "",$searchq);
$query = mysql_query("SELECT * FROM track WHERE keywords LIKE '%$searchq%'");
$count = mysql_num_rows($query);
if($count == 0) {
$output = 'There was no search results!';
} else {
while($row = mysql_fetch_array($query)) {
$consignee = $row['consignee'];
$desti = $row['desti'];
$date = $row['date'];
$id = $row['id'];
}
}
}
?>
答案 0 :(得分:0)
您需要将html放在表单中,并且只需要设置传统提交按钮的样式。
如果您不想创建表单,那么您可能可以通过Javascript实现您在此处所做的任何事情。
但PHP需要您使用<form></form>
试试这个:
<form method="post">
<input type="text" class="input-fullwidth" name="track" id="track" />
<input type="submit" id="track-it" name="submit" class="btn-custom btn-fullwidth" />
</form>
并在您的PHP中检查是否已提交
if(isset($_POST['submit'])) {
// your checks and execution goes here
}
通过这种方式,您可以检查表单是否已提交以及曲目输入是否为空,那么您说它不应该是。
答案 1 :(得分:0)
<form id="my_form">
<!-- Your Form -->
<a href="javascript:{}" onclick="document.getElementById('my_form').submit(); return false;">submit</a>
</form>
在此之前,您必须设置<form>