我目前正在开展一个副业项目来进行门票销售。我创建了一个简单的网页,其中包含我需要收集的信息的基础知识
<div id="session" class="tabcontent">
<form action="test.php" method="post" oninput=" totalamount.value = Math.round(adult.value * 10) + Math.round(student.value * 7); changedue.value = Math.round(moneygiven.value - totalamount.value);" id="ticketform">
<fieldset>
<select name=“ticketform” id="ticketform" form="ticketform">
<option value="" disabled="disabled" selected="selected">Performance</option>
<option value=“1“ type="number" name="showtime1">Show 1 - May 9th 2017</option>
<option value=“2“ type="number" name="showtime2">Show 2 - May 10th 2017</option>
<option value=“3” type="number" name="showtime3">Show 3 - May 11th 2017</option>
</select>
<h4>Ammount of Adults</h4>
<input name="adult" id="adult" type="number">
<br />
<h4>Ammount of Students</h4>
<input name="student" id="student" type="number">
<br />
<h4>Money Owed</h4>
<output name="totalamount" id="totalamount" for="adult student"></output>
<br />
<h4>Money Given</h4>
<input name="moneygiven" id="moneygiven" type="number">
<h4>Change due</h4>
<output name="changedue" id="changedue" for="moneyowed moneygiven"></output>
<input type="submit" />
</fieldset>
</form>
<footer>
</footer>
但是我遇到了一些问题,作为输出的字段和标签选项不会发送到我的数据库。
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "TicketSales";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$showtime=$_POST['showtime'];
$adult=$_POST['adult'];
$student=$_POST['student'];
$totalamount=$_POST['totalamount'];
$moneygiven=$_POST['moneygiven'];
$changedue=$_POST['changedue'];
// Insert data into database
$sql="INSERT INTO tickets (showtime, adult, student, totalamount, moneygiven, changedue)
VALUES
('$showtime', '$adult', '$student', '$totalamount', '$moneygiven', '$changedue')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
这是我的发送表单。一切都是链接的,任何带有输入标签的东西都可以正常工作。我怎样才能让它也发送选择值和输出?
最佳答案
您正在使用 POST 方法,因此每当表单提交时 $_POST 数组都会设置,该数组使用元素名称作为键,使用元素内的值作为该键的值。
因此,当您使用 name=“ticketform”
时,$_POST 将设置为 $_POST['ticketform'] = selected_value
<select name=“ticketform” id="ticketform" >
<option value="" disabled="disabled" selected="selected">Performance</option>
<option value=“1“>Show 1 - May 9th 2017</option>
<option value=“2“ >Show 2 - May 10th 2017</option>
<option value=“3”>Show 3 - May 11th 2017</option>
</select>
$showtime=$_POST['ticketform'];
关于javascript - <select> 标签未发布到 MySql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46640412/