简体   繁体   中英

Can't get data from Dropdown List PHP to Xampp MySQL

I'm trying to get that from a dropdown list from my html.

<FORM ID="" ACTION="save.php">
            <TABLE BORDER=1 WIDTH=90%>
                    <TR>
                        <TH><p style="color:red">Time</p></TH>
                    </TR>
                <TR>  
                    <TD>
                    <SELECT NAME="Time" ID="Time" style="width:220px">
                        <OPTION>
                        <OPTION>7:30-8:30
                        <OPTION>7:30-9:00
                        <OPTION>7:30-10:30
                    </SELECT>
                    </TD>

What happens is when the user click the submit button, it automatically call save.php

<?php
$time = $_POST['Time'];

//create connection_aborted
$conn = mysqli_connect("localhost", "root", "", "scheduling");
//check connection
if($conn-> connect_error) {
    die ("connection failed; ". $conn-> connect_error);
}
$sql = "INSERT INTO sched (Time)VALUES ('$time')";

$conn->close();
?>

This technique works on textbox, I don't know why can't get data from drop down list. Am I missing something? Please help to fix.

Your option tags need a value attribute to send to the server. Try this:

<SELECT NAME="Time" ID="Time" style="width:220px">
  <OPTION value=""></OPTION>
  <OPTION value="7:30-8:30">7:30-8:30</OPTION>
  <OPTION value="7:30-9:00">7:30-9:00</OPTION>
  <OPTION value="7:30-10:30">7:30-10:30</OPTION>
</SELECT>

You need set the option value like this:

<select id="Time" required>
  <option value="">Please Select</option>
  <option value="7:30-8:30">7:30-8:30</option>
  <option value="7:30-9:00">7:30-9:00</option>
  <option value="7:30-10:30">7:30-10:30</option>
</select> 

And if you have option with blank value, good to check in php first before insert like this

if (!empty($_POST['Time'])) $Time = $_POST['Time'];

<select id="Time" name="Time">
  <option disabled="disabled" selected="selected" value="">Please Select One</option>
  <option value="7:30-8:30">7:30-8:30</option>
  <option value="7:30-9:00">7:30-9:00</option>
  <option value="7:30-10:30">7:30-10:30</option>
</select>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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