PHP使用选择值搜索并将结果显示到文本框

时间:2014-02-20 05:16:15

标签: php html mysql search

飞机桌上的我的选择框项目

<select name="selectaircraft">
<option id="0">-- Select Aircraft -- </option>
<?php
require("dbc.php");
$getallaircraft = mysql_query("SELECT * FROM aircrafttable");
while($viewallaircraft = mysql_fetch_array($getallaircraft)){
?>
<option id="<?php echo $viewallaircraft['ID']; ?>">
<?php echo $viewallaircraft['aircraft'] ?> </option>
<?php } ?>
</select>

大家好我试图使用selectaircraft的值来搜索并将结果显示到文本框..这是我的代码到目前为止

<?php
   require("dbc.php");
       $getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE 'PR200'");  
   while($viewallconfig= mysql_fetch_array($getallconfig)){

?>
   <input type="text" name="aconfig "value="<?php echo $viewallconfig['config'];      ?>" />
<?php } ?>

正如你所看到的,我只把PR200用于搜索,我的问题是如何使用select值代替?我还希望每次单击选择框时都会发生搜索查询。

伙计们可以帮助我吗?我知道如何在VB6中编写代码,就像这样

假设:选择名称或在VB6中使用combobox = selectbox

Private Sub Combo3_Click()
adodc1.recorsource ="Select * from aircrafttable where aircraft = " & selectbox & "'"
adodc1.refresh


Text1.Text = Adodc1.Recordset("aircraft")

每次点击combo / selectbox时,查询都会执行并给文本框提供不同的答案。 我不知道如何在PHP中执行此操作帮助!

已编辑添加了所有代码

    <form action="addrecord.php" method="post" >


    <table border=1>
    <tr><td bgcolor="#999999">
    <strong>Area:</strong> </td>
    <td>

 <input type="text" required="required" name="aarea" size=10 /><br /></td></tr>

    <tr><td bgcolor="#999999">
    <strong>Aircraft:</strong></td>





<td>

<form method="post" action="aircraft.php">
 <select name="selectaircraft" onchange="this.form.submit();">
     <option id="0">Aircraft</option>
<?php
      require("dbc.php");
      $getallaircraft = mysql_query("SELECT * FROM aircrafttable");
      while($viewallaircraft = mysql_fetch_array($getallaircraft)){
?>
      <option id="<?php echo $viewallaircraft['ID']; ?>">
<?php 
      echo $viewallaircraft['aircraft'] ?> </option>
<?php } ?>
    </select>



<?php
        require("dbc.php");
        if(isset($_POST)){
        $takeaircraft = mysql_real_escape_string($_POST['selectaircraft']);     

{

?>
   <input type="text" name="aaircraft" size=3 value="<?php echo $takeaircraft;  ?>" />
<?php } 
}?>


</form>


</td></tr>

<tr><td bgcolor="#999999">
<strong>Flight:</strong> </td>
 <td><input type="text"  name="aflight" size=10  /><br /></td></tr>

 <tr><td bgcolor="#999999">
<strong>Configuration:</strong> </td>
 <td>



 <?php
        require("dbc.php");
        if(isset($_POST)){
        $selectaircraft = mysql_real_escape_string($_POST['selectaircraft']);     
         $getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE '".$selectaircraft."'"); 
 while($viewallconfig= mysql_fetch_array($getallconfig)){

?>
   <input type="text" name="aconfig" value="<?php echo $viewallconfig['config'];      ?>" />
<?php } 
}?>

 <br /></td></tr>
  <tr><td bgcolor="#999999">
<strong>Month:</strong> </td>
 <td>
 <select name="amonth">
        <option value="na">Month</option>
        <option value="January">January</option>
   </select>
 </td></tr>

 <tr><td bgcolor="#999999">
<strong>Frequency:</strong> </td>
 <td><span style="font-size:11px; font-weight:bolder" >

    Mon<input type="checkbox" id='check1' onClick='checkmon()'>
<input type="hidden"  id="txt1" name="hiddenmon" value="n/a">


   Tue<input type="checkbox" id='check2' onClick='checktue()'>
<input type="hidden"  id="txt2" name="hiddentue" value="n/a">

   Wed<input type="checkbox" id='check3' onClick='checkwed()'>
<input type="hidden"  id="txt3" name="hiddenwed"  value="n/a">


   Thu<input type="checkbox" id='check4' onClick='checkthu()'>
<input type="hidden"  id="txt4" name="hiddenthu" value="n/a">

  Fri<input type="checkbox" id='check5' onClick='checkfri()'>
<input type="hidden"  id="txt5" name="hiddenfri"  value="n/a">

  Sat<input type="checkbox" id='check6' onClick='checksat()'>
<input type="hidden"  id="txt6" name="hiddensat"  value="n/a">

  Sun<input type="checkbox" id='check7' onClick='checksun()'>
<input type="hidden"  id="txt7" name="hiddensun" value="n/a">


 </span>
 <br /></td></tr>



<tr><td bgcolor="#999999">
<strong>Menu:</strong> </td>
<td><input type="text" name="amenu" size=10 /><br /></td></tr>



<tr><td bgcolor="#999999">
<strong>Cycle:</strong> </td>
 <td>
 <select name="acycle">
        <option value="na">Cycle</option>
        <option value="Cycle 1">Cycle 1</option>

        </select>


<tr><td bgcolor="#999999">
<strong>Items:</strong> </td>
 <td>
 <select name="aitem">
        <option value="na">Items</option>

                </select>



<tr><td bgcolor="#999999">
<strong>STD Time:</strong> </td>
<td><input type="text" name="astdtime" size=5 /><br /></td></tr>


<tr><td bgcolor="#999999">
<strong>Quantity:</strong> </td>
<td><input type="text" name="aqty" size=5 /><br /></td></tr>


<tr>
<td colspan="2" align="right" bgcolor="#999999">
<input type='submit' name="add" value="Add Records" />
</td>
</tr>
 </table>



    </form>

3 个答案:

答案 0 :(得分:0)

试试这样;

$aircraft = mysql_real_escape_string($_POST['selectaircraft']);  //or $_GET

$getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE '".$aircraft."'");

答案 1 :(得分:0)

您可以将选择框(第1个)保留在表单中,在更改时将表单提交到下一页。

获取POST / GET值,并将其用于选择查询以进行搜索。

答案 2 :(得分:0)

触发提交更改选择框如下

<form action="path/to/your/query/file.php" method="post"> //if query in same page leave your action blank
   <select name="selectaircraft" onchange="this.form.submi();">
     <option id="0">-- Select Aircraft -- </option>
<?php
      require("dbc.php");
      $selected_aircraft=isset($_POST['selectaircraft'])?$_POST['selectaircraft']:"";
      $getallaircraft = mysql_query("SELECT * FROM aircrafttable");
      while($viewallaircraft = mysql_fetch_array($getallaircraft)){
      $selectstr="";
      if($selected_aircraft==$viewallaircraft['aircraft'] ){
         $selectstr="selected='selected'";
      }

?>
      <option id="<?php echo $viewallaircraft['ID']; ?>" <?php echo $selectstr ?> >
<?php 
             echo $viewallaircraft['aircraft'] ?> </option>
<?php } ?>
    </select>
</form>

并按以下方式更改您的查询:

 <?php
        require("dbc.php");
        if(isset($_POST)){
        $selectaircraft = mysql_real_escape_string($_POST['selectaircraft']);     
         $getallconfig = mysql_query("SELECT * FROM aircrafttable WHERE aircraft LIKE '{ $selectaircraft}'"); 
 while($viewallconfig= mysql_fetch_array($getallconfig)){

?>
   <input type="text" name="aconfig "value="<?php echo $viewallconfig['config'];      ?>" />
<?php } 
}?>

更新更新的代码以维护选择框的状态。