1

我有个问题。我需要从选择标记中获取值,然后在 php 中将其用于我的 sql。这是我的代码

<div class="form-group">
 <label> ROOMS </label>
     <?php     
                echo "<select value= 'TRoom1' id ='TRoom1' class='form control'>";
                      echo "<option>Select Room Type</option>";
                       while ($row1 = mysql_fetch_array($result2))
                       {

                          echo "<option>" . $row1['Room_type'] . "</option>";

                       }
                       echo "</select>";
                   ?>

这是用于 sql 命令的

         <div class="modal-body">
             <div class="container">
                <?php
                  $selectedValue = $_POST['TRoom1'];
            $sql = "SELECT  RoomNumber FROM rooms Where Room_type = '$selectedValue' ";
                  $result = mysql_query($sql);
                   echo "<select value= 'RoomNo' id ='RoomID' class='form-control'>";
                      echo "<option>Select Room Number</option>";
                       while ($row = mysql_fetch_array($result))
                       {

                          echo "<option>" . $row['RoomNumber'] . "</option>";
                       }
                       echo "</select>";
                   ?>

蒂亚!:))

这是房间类型的代码及其对应的房间号

                           <div class="form-group">
              <label for="exampleInputEmail1"> ROOMS </label>
                    <?php

     echo "<select value= 'TRoom1' name ='TRoom1' id ='TRoom1' class='form-control'>";
                      echo "<option>Select Room Type</option>";
                       while ($row1 = mysql_fetch_array($result2))
                       {

                          echo "<option>" . $row1['Room_type'] . "</option>";

                       }
                       echo "</select>";
                   ?>

                 </div>

      <div class="form-group">
       <?php
                 $select_value=$_POST['selectedValue'];
          $sql = "SELECT  RoomNumber FROM rooms Where Room_type = '$select_value' ";
                  $result = mysql_query($sql);

                 echo "<select value= 'RoomNo' id ='RoomID' class='form-control'>";
                      echo "<option>Select Room Number</option>";
                       while ($row = mysql_fetch_array($result))
                       {

                          echo "<option>" . $row['RoomNumber'] . "</option>";

                       }
                       echo "</select>";
                   ?>
                </div>
4

4 回答 4

0

试试喜欢

var Sel_val = document.getElementById('TRoom1').value;

Sel_val将是该下拉菜单的选定值。ajax在您的情况下使用更好。如果它在同一页面上,那么您使用Form submit方法。

对于 ajax,首先你需要目标 url 和你想要发送的值。所以试试

$.ajax({
    url : Url of the page at which the sql command will be there,
    type : 'POST',
    data : { Sel_val : Sel_val }
});

然后在您的目标文件中获取Sel_valviaPOST方法。

于 2013-10-25T04:56:34.817 回答
0

请在此处发布之前搜索您的疑问。有很多可用的例子。mysql_query已弃用使用mysqli_功能

  <div class="form-group">
  <label> ROOMS </label>
  <?php     
            echo "<select id ='TRoom1' name ='TRoom1' class='form control'>";
                  echo "<option>Select Room Type</option>";
                   while ($row1 = mysql_fetch_array($result2))
                   {

                      echo "<option value=".$row1['Room_type'].">" . $row1['Room_type'] . "</option>";

                   }
                   echo "</select>";
               ?>

如果您提交表单,post您将获得如下值

 $sql = "SELECT Room_type, Rate, RoomNumber FROM rooms Where Room_type ='".$_POST['TRoom1']."' ";
于 2013-10-25T05:00:58.480 回答
0

我认为您使用了自我操作并尝试以下代码

        if($_POST){     
        $selectedValue = $_POST['TRoom1'];
        $sql = "SELECT  RoomNumber FROM rooms Where Room_type = '$selectedValue' ";
              $result = mysql_query($sql);
               echo "<select value= 'RoomNo' id ='RoomID' class='form-control'>";
                  echo "<option>Select Room Number</option>";
                   while ($row = mysql_fetch_array($result))
                   {

                      echo "<option>" . $row['RoomNumber'] . "</option>";
                   }
                   echo "</select>";
        }   
于 2013-10-25T05:01:01.583 回答
0

如果您想在 php 部分中获取值并且在选项中您必须传递值 attibute.that 值您将在 php 部分中获取,您需要为您的选择标签使用名称属性。

<html>
<head></head>
<body>

 <form action="a.php" method="post">
 <select name="selectname" id="someid" >
 <?php
 while ($row1 = mysql_fetch_array($result2))
 {
 ?>
 <option value="<?php echo $varible ?>"> <?php echo $row1['Room_type']; ?></option>

 <?php } ?>                
</select>
<input type="submit" value="submit">
</form>
</body>
</html>

对于 php 部分:你可以像这样获取值:filename=a.php

<?php
$select_value=$_REQUEST['selectname'];
$sql1 = "SELECT  RoomNumber FROM rooms Where Room_type = '$select_Value' ";
$sql=mysql_result(mysql_query($sql1),0);
?>
于 2013-10-25T05:07:02.690 回答