仅在选择了来自下拉菜单的值时才显示下拉菜单的结果 [英] display result from dropdown menu only when value from it is selected
问题描述
这段代码是我搜索表单的演示部分。实际的搜索表单包含3个下拉列表,从下拉列表中选择的值充当关键字,基于哪个搜索进行。
此下拉列表包含芒果,苹果,葡萄等水果。搜索代码工作正常。但问题是,下拉列表中显示的第一个选项是Select(不包含任何值),低于该选项的水果的实际列表开始,但仍然会在页面首次加载时选择第一个水果的值时间 。我想要做的是,当页面加载第一次,即在下拉列表中的值是选择什么都不应该显示,然后当用户从下拉列表中选择值并点击提交按钮,然后只有结果应显示
< div class =col-md-3 col-sm-5>
< div class =media>
< div class =media-body>
<?php
$ servername =localhost;
$ username =root;
$ password =;
$ dbname =db;
//创建连接
$ con = mysqli_connect($ servername,$ username,$ password,$ dbname);
//检查连接
if(!$ con){
die(Connection failed:。mysqli_connect_error());
}
$ sql =从水果中选择水果;
$ result = $ con> query($ sql);
echo< label for ='fruits'>处理类型:< / label>;
echo< select name ='fruits'id ='fruits'class ='form-control'>< option value =''> - Select - < / option>;
while($ row = $ result-> fetch_assoc()){
echo< option value ='。 $ row ['fruits']。 >中。 $ row ['fruits']。 < /选项> 中;
}
回显< / select>;
?>
< / div>
< / div>
< / div>
执行搜索部分的代码
<$ p $ mysqli_connect(,,,); //检查连接
if(mysqli_connect_errno())
{
echo无法连接到MySQL:。 mysqli_connect_error();
}
$ fruits = mysqli_real_escape_string($ con,$ _POST ['fruits']);
$ sql1 =选择*从处理水果LIKE'%$ fruits%';
$ result = mysqli_query($ con,$ sql1);
echo< table class ='table table-striped table-bordered responsive>
< thead>
< tr>
< th> < />
< th>类型< / th>
< th>水果>
< / tr>
< / thead>
$ b $ if(mysqli_num_rows($ result)> 0)
{
while($ row = mysqli_fetch_assoc($ result))
{
echo < tbody data-link ='row'class ='rowlink'>;
回显< tr>;
echo< td>< a href ='#'> 。 $ row ['name']。 < / A>< / TD> 中;
回显< td> 。 $ row ['type']。 < / TD> 中;
回显< td> 。 $ row ['fruits']。 < / TD> 中;
echo< / tr>;
回声< / tbody>;
}
}
else
{
echo0 results;
}
echo< / table>;
mysqli_close($ con);
?>
希望有人能指导我
PS(编辑部分)
< div class =col-md-3 col-sm-5>
< div class =media>
< div class =media-body>
<?php
$ servername =localhost;
$ username =root;
$ password =;
$ dbname =db;
//创建连接
$ con = mysqli_connect($ servername,$ username,$ password,$ dbname);
//检查连接
if(!$ con)
{
die(Connection failed:mysqli_connect_error());
}
$ sql =从水果中选择水果;
$ result = $ con> query($ sql); ?>
echo< label for =fruits>处理类型:< / label>;
echo< select name =fruitsid =fruitsclass =form-control>
< option value =<?php if(!isset($ _ POST ['fruits'])){?>选择<?php}?>> - 选择 - < / option>;
<?php
while($ row = $ result-> fetch_assoc()){?>
echo< option value =<?php echo $ row ['fruits']; ?><?php if(isset($ _ POST ['fruits'])&& $ _POST ['fruits'] == $ row ['fruits']){?> selected<?php} ?>><?php echo $ row ['fruits'];?>< / option>;
<?php}?>
< / select>
< / div>
< / div>
< / div>
在< option> ;
部分,只需修改一下。用条件添加 selected
:
< div class =col- md-3 col-sm-5>
< div class =media>
< div class =media-body>
<?php
$ servername =localhost;
$ username =root;
$ password =;
$ dbname =db;
//创建连接
$ con = mysqli_connect($ servername,$ username,$ password,$ dbname);
//检查连接
if(!$ con){
die(Connection failed:。mysqli_connect_error());
}
$ sql =从水果中选择水果;
$ result = $ con> query($ sql); ?>
< label for =fruits>处理类型:< / label>
< option value =<?php if(!isset($ _ POST ['fruits'])||(isset($ _ POST ['fruits'])&& empty($ _ POST [ 'fruits']))){?>选择<?php}?>> - 选择 - < / option>
<?php
while($ row = $ result-> fetch_assoc()){
?>
< option value =<?php echo $ row ['fruits'];?> <?php if(isset($ _ POST ['fruits'])&& $ _POST ['fruits'] == $ row ['fruits']){?> selected<?php}?>>> ;<?php echo $ row ['fruits']; ?>< /选项>
<?php}?>
< / select>
< / div>
< / div>
< / div>
结果页:
<?php
$ con = mysqli_connect(,,,); //检查连接
if(mysqli_connect_errno ())
echo无法连接到MySQL:。 mysqli_connect_error();
$ fruits = mysqli_real_escape_string($ con,$ _POST ['fruits']);
if(!empty($ fruits)){
$ sql1 =SELECT * FROM treatment where果子LIKE'%$ fruits%';
$ result = mysqli_query($ con,$ sql1);
$ count = mysqli_num_rows($ result);
}
else
$ count = 0; ?>
< table class ='table table-striped table-bordered responsive'>
< thead>
< tr>
< th>名称< / th>
< th>类型< / th>
< th>水果< / th>
< / tr>
< / thead>
<?php
if($ count> 0){
while($ row = mysqli_fetch_assoc($ result)){
?>
< tbody data-link ='row'class ='rowlink'>
< tr>
< td>< a href ='#'><?php echo $ row ['name']; ?>< / A>< / TD>
< td><?php echo $ row ['type']; ?>< / TD>
< td><?php echo $ row ['fruits']; ?>< / TD>
< / tr>
< / tbody>
<?php
}
}
else
echo0 results;
?>
< / table>
<?php mysqli_close($ con); ?>
This code is a demo part of my search form. the actual search form contains 3 dropdown list, values selected from the dropdown list acts as a keyword on basis of which search is conducted. This dropdown list contains fruits such as mango, apple, grapes etc. search code is working fine. But the issue is that the first option displayed in the dropdown list is Select (that holds no value), below which the actual list of fruit starts, but still the value of first fruit is getting selected when the page is getting loaded for the first time . What i am trying to do is that when the page loads for the first time i.e the value in the drop down list is Select nothing should get displayed and after that when the user selects the value from dropdown and hits the submit button then only then result should get displayed
<div class="col-md-3 col-sm-5">
<div class="media">
<div class="media-body">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT fruits FROM fruits";
$result = $con->query($sql);
echo "<label for='fruits'>Treatment Type: </label>";
echo "<select name='fruits' id='fruits' class='form-control'><option value=''>--Select--</option>";
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['fruits'] . "'>" . $row['fruits'] . "</option>";
}
echo "</select>";
?>
</div>
</div>
</div>
Code that does the search part
<?php
$con=mysqli_connect("","","","");// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$fruits = mysqli_real_escape_string($con, $_POST['fruits']);
$sql1 = "SELECT * FROM treatment WHERE fruits LIKE '%$fruits%'";
$result = mysqli_query($con, $sql1);
echo "<table class='table table-striped table-bordered responsive'>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Fruits</th>
</tr>
</thead>";
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
echo "<tbody data-link='row' class='rowlink'>";
echo "<tr>";
echo "<td><a href='#'>" . $row['name'] . "</a></td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['fruits'] . "</td>";
echo "</tr>";
echo "</tbody>";
}
}
else
{
echo "0 results";
}
echo "</table>";
mysqli_close($con);
?>
Would appreciate if someone can guide me
P.S (edited part)
<div class="col-md-3 col-sm-5">
<div class="media">
<div class="media-body">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$con)
{
die("Connection failed: " mysqli_connect_error());
}
$sql = "SELECT fruits FROM fruits";
$result = $con->query($sql); ?>
echo "<label for="fruits">Treatment Type: </label>";
echo "<select name="fruits" id="fruits" class="form-control">
<option value="" <?php if(!isset($_POST['fruits'])) { ?>selected<?php } ?>>--Select--</option>";
<?php
while($row = $result->fetch_assoc()) { ?>
echo "<option value="<?php echo $row['fruits']; ?>" <?php if(isset($_POST['fruits']) && $_POST['fruits'] == $row['fruits']) { ?>selected<?php } ?>><?php echo $row['fruits']; ?></option>";
<?php } ?>
</select>
</div>
</div>
</div>
In the <option>
portions, just modify a bit. Add selected
with conditions:
<div class="col-md-3 col-sm-5">
<div class="media">
<div class="media-body">
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT fruits FROM fruits";
$result = $con->query($sql); ?>
<label for="fruits">Treatment Type: </label>
<select name="fruits" id="fruits" class="form-control">
<option value="" <?php if(!isset($_POST['fruits']) || (isset($_POST['fruits']) && empty($_POST['fruits']))) { ?>selected<?php } ?>>--Select--</option>
<?php
while($row = $result->fetch_assoc()) {
?>
<option value="<?php echo $row['fruits']; ?>" <?php if(isset($_POST['fruits']) && $_POST['fruits'] == $row['fruits']) { ?>selected<?php } ?>><?php echo $row['fruits']; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
Results page:
<?php
$con = mysqli_connect("","","","");// Check connection
if(mysqli_connect_errno())
echo "Failed to connect to MySQL: " . mysqli_connect_error();
$fruits = mysqli_real_escape_string($con, $_POST['fruits']);
if(!empty($fruits)) {
$sql1 = "SELECT * FROM treatment WHERE fruits LIKE '%$fruits%'";
$result = mysqli_query($con, $sql1);
$count = mysqli_num_rows($result);
}
else
$count = 0; ?>
<table class='table table-striped table-bordered responsive'>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Fruits</th>
</tr>
</thead>
<?php
if ($count > 0) {
while($row = mysqli_fetch_assoc($result)) {
?>
<tbody data-link='row' class='rowlink'>
<tr>
<td><a href='#'><?php echo $row['name']; ?></a></td>
<td><?php echo $row['type']; ?></td>
<td><?php echo $row['fruits']; ?></td>
</tr>
</tbody>
<?php
}
}
else
echo "0 results";
?>
</table>
<?php mysqli_close($con); ?>
这篇关于仅在选择了来自下拉菜单的值时才显示下拉菜单的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!