简体   繁体   English

使用php和MySql在HTML中下拉列表

[英]Drop down list in html using php and MySql

I am trying to create a dropdown list that uses php to connect to a MySql database. 我正在尝试创建一个使用php连接到MySql数据库的下拉列表。 But it's not working and I'm sure it's a simple change I need to make but because of the complicated fancy bootstrap form, I can't figure it out! 但这不起作用,我确定这是我需要做的简单更改,但是由于复杂的花式引导程序形式,我无法弄清楚!

Here is the code I have: 这是我的代码:

<form method="post" action="bookings.php" class="popup-form">
                    <fieldset>
          <input id="email" name="email" type="text" class="form-control form-white" placeholder="Email">
                    <input id="firstname" name="firstname" type="text" class="form-control form-white" placeholder="Firstname">
                    <input id="lastname" name="lastname" type="text" class="form-control form-white" placeholder="Surname">
          <div class="dropdown">
                        <button id="dLabel" for="retreatseleted" class="form-control form-white dropdown" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            Select Retreat:
                        </button>
                        <ul for="retreatseleted" class="dropdown-menu animated fadeIn" role="menu" aria-labelledby="dLabel">
                            <li id="retreatseleted" name="retreatseleted" value="Clifftop Cottage" class="animated lightSpeedIn"><a href="#">Clifftop Cottage</a></li>
                            <li id="retreatseleted" name="retreatseleted" value="Cherry Trees" class="animated lightSpeedIn"><a href="#">Cherry Trees</a></li>
              <li id="retreatseleted" name="retreatseleted" value="The Beach House" class="animated lightSpeedIn"><a href="#">The Beach House</a></li>
                        </ul>
                    </div>
          <input id="fromdate" name="fromdate" type="date" class="form-control form-white" placeholder="From Date:">
          <input id="todate" name="todate" type="date" class="form-control form-white" placeholder="To Date:">
                    <div class="checkbox-holder text-left">
                        <div class="checkbox">
                            <input type="checkbox" value="None" id="squaredOne" name="check" />
                            <label for="squaredOne"><span>I Agree to the <strong>Terms &amp; Conditions</strong></span></label>
                        </div>
                    </div>
                    <button type="submit" value="Submit" class="btn btn-submit">Submit</button>
                    </fieldset>
                </form>

And the php code I have is: 我拥有的php代码是:

ob_start();
session_start();

include("connectdb.php");

unset($_SESSION['error']);

  if (!empty($_POST)
   && !empty($_POST['email'])
   && !empty($_POST['firstname'])
   && !empty($_POST['lastname'])
   && !empty($_POST['retreatselected'])
   && !empty($_POST['fromdate'])
   && !empty($_POST['todate'])) {

    $email = $_POST['email'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $retreatselected = $_POST['retreatseleted'];
    $fromdate = $_POST['fromdate'];
    $todate = $_POST['todate'];

    $query = "SELECT COUNT(*) AS count FROM bookings WHERE fromdate = '".$fromdate."' and retreatselected = '".$retreatselected."'";
    $result = $db->query($query);
    $data = $result->fetch_assoc();


    if ($data['count'] > 0) {

      $_SESSION['error'] = "Unfortunately this date has already been taken at $retreatselected.";
      header("location: index.php");


    } else {


      $query = "INSERT INTO bookings (email, firstname, lastname, retreatselected, fromdate, todate)
                VALUES ('".$email."','".$firstname."','".$lastname."','".$retreatselected."','".$fromdate."','".$todate."')";
      "</p>";


      $result = $db->query($query);


      if ($result) {

        $_SESSION['thankyou'] = "Thank you for booking $retreatselected. You will receive a confirmation email shortly.";
            header("location: index.php");

      } else {
          echo "SQL Error: " . $db->error;
      }
    }
  }
?>

It works perfectly when I take out the dropdown list and 'retreatselected' variable. 当我取出下拉列表和“ retreatselected”变量时,它可以完美地工作。 I have a column in my database for retreatselected and I want the value the user chooses to appear in the database. 我的数据库中有一列可供选择,我希望用户选择的值出现在数据库中。

I'm pretty sure it's a simple id=/name=/value= correction to make but I'm going mad trying every possibility! 我很确定这是一个简单的id = / name = / value =校正,但是我会为每种可能性而疯狂!

Any help greatly appreciated :) 任何帮助,不胜感激:)

To keep twitter bootstrap styling of any HTML form element, you must give it a class of "form-control". 要使Twitter引导程序样式保持任何HTML表单元素的样式,必须为它提供一个“ form-control”类。 This applies to: 这适用于:

  • input (text) 输入文本)
  • input (password) 输入密码)
  • input (radio) 输入(无线电)
  • input (checbox) 输入(复选框)
  • select 选择
  • textarea 文本区域

So, to follow on from Rigg's answer, you should use: 因此,要遵循Rigg的答案,您应该使用:

Select Retreat:
<select name="retreatseleted" id="retreatseleted" class="form-control">
    <option value="Clifftop Cottage">Clifftop Cottage</option>
    <option value="Cherry Tree">Cherry Trees</option>
    <option value="The Beach House">The Beach House</option>
</select>

Hope this helps! 希望这可以帮助!

The reason is that the retreatselected's are not form input fields, so there will be no data called $_POST['retreatselected'] in the $_POST array. 原因是撤退选择的不是表单输入字段,因此$_POST数组中将没有名为$_POST['retreatselected']数据。 Unless there is some javascript doing something clever in the background you are not telling us about, 除非有一些javascript在后台执行巧妙的操作,否则您不会告诉我们,

Replace 更换

<button id="dLabel" for="retreatseleted" class="form-control form-white dropdown" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Select Retreat:
</button>
<ul for="retreatseleted" class="dropdown-menu animated fadeIn" role="menu" aria-labelledby="dLabel">
    <li id="retreatseleted" name="retreatseleted" value="Clifftop Cottage" class="animated lightSpeedIn"><a href="#">Clifftop Cottage</a></li>
    <li id="retreatseleted" name="retreatseleted" value="Cherry Trees" class="animated lightSpeedIn"><a href="#">Cherry Trees</a></li>
    <li id="retreatseleted" name="retreatseleted" value="The Beach House" class="animated lightSpeedIn"><a href="#">The Beach House</a></li>
</ul>

With

Select Retreat:
<select name="retreatseleted" id="retreatseleted">
    <option value="Clifftop Cottage">Clifftop Cottage</option>
    <option value="Cherry Tree">Cherry Trees</option>
    <option value="The Beach House">The Beach House</option>
</select

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM