运行此命令时,提交表单后,我将检查数据库,并且复选框的所有值(smalltube,largetube,wakeboard)都表示是,即使未选中也是如此。
知道为什么吗?
复选框应从此处获取是或否值
//add info here for the check boxes.
if(empty (trim ($_POST["smallTube"]))){
$smallTube= "no";
}else{
$smallTube = "yes";
}
//add info here for the check boxes.
if(empty (trim ($_POST["largeTube"]))){
$largeTube= "no";
}else{
$largeTube = "yes";
}
//add info here for the check boxes.
if(empty (trim ($_POST["wakeboard"]))){
$wakeboard= "no";
}else{
$wakeboard = "yes";
}
这里是完整页面代码。
<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: Login.php");
exit;
}
//Include config file
require_once "config.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Wake Up Rentals</title>
<meta charset="utf-8">
<meta name = "format-detection" content = "telephone=no" />
<link rel="icon" href="images/favicon.ico">
<link rel="shortcut icon" href="images/favicon.ico" />
<link rel="stylesheet" href="booking/css/booking.css">
<link rel="stylesheet" href="css/camera.css">
<link rel="stylesheet" href="css/owl.carousel.css">
<link rel="stylesheet" href="css/style.css">
<script src="js/jquery.js"></script>
<script src="js/jquery-migrate-1.2.1.js"></script>
<script src="js/script.js"></script>
<script src="js/superfish.js"></script>
<script src="js/jquery.ui.totop.js"></script>
<script src="js/jquery.equalheights.js"></script>
<script src="js/jquery.mobilemenu.js"></script>
<script src="js/jquery.easing.1.3.js"></script>
<script src="js/owl.carousel.js"></script>
<script src="js/camera.js"></script>
<!--[if (gt IE 9)|!(IE)]><!-->
<script src="js/jquery.mobile.customized.min.js"></script>
<!--<![endif]-->
<script src="booking/js/booking.js"></script>
<script>
$(document).ready(function(){
jQuery('#camera_wrap').camera({
loader: false,
pagination: false ,
minHeight: '444',
thumbnails: false,
height: '28.28125%',
caption: true,
navigation: true,
fx: 'mosaic'
});
$().UItoTop({ easingType: 'easeOutQuart' });
});
</script>
<script type="text/javascript" src="js/crlist.js"> </script>
<script>
$(document).ready(function() {
initCs("");
});
</script>
</head>
<?php
// Define variables and initialize with empty values
$lake = $days = $dock = $date = $numberofsports = $numberofdeluxe = $smallTube = $largeTube = $wakeboard= "";
$lake_err = $days_err = $date_err = $numberofsports_err = $numberofdeluxe_err =$smallTube_err = $largeTube_err = $wakeboard_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
//Validate lake
if(empty (trim ($_POST["lake"]))){
$lake_err = "Please select a lake";
}else{
$lake = trim($_POST["lake"]);
}
//Validate days
if(empty (trim ($_POST["days"]))){
$days_err = "Please select number of days";
}else{
$days = trim($_POST["days"]);
}
//Validate date
if(empty (trim ($_POST["date"]))){
$date_err = "Please select a start date";
}else{
$date = trim($_POST["date"]);
}
//Validate number of sports
if (!in_array(trim($_POST["numberofsports"]), ["0", "1", "2", "3", "4"])) {
$numberofsports_err = "Please select an amount";
}else{
$numberofsports = trim($_POST["numberofsports"]);
}
//Validate nmber of deluxe
if (!in_array(trim($_POST["numberofdeluxe"]), ["0", "1", "2"])) {
$numberofdeluxe_err = "Please select an amount";
}else{
$numberofdeluxe = trim($_POST["numberofdeluxe"]);
}
//add info here for the check boxes.
if(empty (trim ($_POST["smallTube"]))){
$smallTube= "no";
}else{
$smallTube = "yes";
}
//add info here for the check boxes.
if(empty (trim ($_POST["largeTube"]))){
$largeTube= "no";
}else{
$largeTube = "yes";
}
//add info here for the check boxes.
if(empty (trim ($_POST["wakeboard"]))){
$wakeboard= "no";
}else{
$wakeboard = "yes";
}
//dock
$dock = $_POST["ndock"];
echo $_POST["ndock"];
if(empty($lake_err) && empty($days_err) && empty($date_err) && empty($numberofsports_err) && empty($numberofdeluxe_err) ){
// Prepare an insert statement
$sql = "INSERT INTO prerental (email, firstname, lastname, address, phonenumber,lake,days,date,numberofsports,numberofdeluxe,smalltube,largetube,wakeboard,dock) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ssssssssssssss", $param_email, $param_firstname, $param_lastname, $param_address, $param_phonenumber, $param_lake, $param_days, $param_date, $param_numberofsports, $param_numberofdeluxe, $param_smalltube, $param_largeTube, $param_wakeboard, $param_dock);
// Set parameters
$param_email = $_SESSION["email"];
$param_firstname = $_SESSION["fname"];
$param_lastname = $_SESSION["lname"];
$param_address = $_SESSION["address"];
$param_phonenumber = $_SESSION["phone"];
$param_lake = $lake;
$param_days = $days;
$param_date = $date;
$param_numberofsports = $numberofsports;
$param_numberofdeluxe = $numberofdeluxe;
$param_smalltube = $smallTube;
$param_largeTube = $largeTube;
$param_wakeboard = $wakeboard;
$param_dock = $dock;
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Redirect to login page
header("location: FinalChecks.php");
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?>
<body class="page1" id="top">
<div class="main">
<!--==============================header=================================-->
<header>
<div class="menu_block ">
<div class="container_12">
<div class="grid_12">
<nav class="horizontal-nav full-width horizontalNav-notprocessed">
<ul class="sf-menu">
<li <a href="index.php"><img src="images/logo.png" alt="wake up rentals"></a></li>
<li class="current"><a href="index.php">Home</a></li>
<li><a href="rental.php">Rentals</a></li>
<li><a href="lakes.php">Lakes</a></li>
<li><a href="Register.php">Rent Now</a></li>
<li><a href="FAQ.php">FAQ's</a></li>
<li><a href="Login.php">Login</a></li>
<li><a href="Contact.php">Contact Us</a></li>
</ul>
</nav>
<div class="clear"></div>
</div>
<div class="clear"></div>
</div>
</div>
<div class="clear"></div>
</header>
<?php //End of header?>
<style type="text/css">
body{ font: 14px sans-serif; text-align: left; }
</style>
<style type="text/css">
body{ font: 14px sans-serif; }
.wrapper{ width: 350px; padding: 20px; }
</style>
<div class="row2">
<div class="column left" style="background-color:white;">
<div class="wrapper">
<div class="page-header">
<h1>Hi, <b><?php echo htmlspecialchars($_SESSION["fname"]); ?></b>. Your ready to order.</h1>
</div>
<p>
<?php echo htmlspecialchars($_SESSION["fname"]); ?><br>
<?php echo htmlspecialchars($_SESSION["lname"]); ?><br>
<?php echo htmlspecialchars($_SESSION["email"]); ?><br>
<?php echo htmlspecialchars($_SESSION["address"]); ?><br>
<?php echo htmlspecialchars($_SESSION["phone"]); ?><br>
</p>
<p>
<a href="sign-uplakenotify.php" class="btn btn-warning">Sign up for lake notification</a>
<br>
<br>
<br>
<a href="sign-upDiscount.php" class="btn btn-warning">Sign up for Discounts</a>
<br>
<br>
<br>
<a href="reset-password.php" class="btn btn-warning">Reset Your Password</a>
<br>
<br>
<br>
<a href="Logout.php" class="btn btn-danger">Sign Out of Your Account</a>
</p>
</div>
</div>
<div class="column left1" style="background-color:white;">
<div class="wrapper">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group">
<label><b>Get your instint quote here!</b></label>
<label> Which lake do you want to go to?</label>
<?php /*
<select class="form-control" name="lake">
<option value="">Select...</option>
<option value="Cherokee">Cherokee</option>
<option value="Douglas">Douglas</option>
<option value="Fort-Loudoun">Fort Loudoun</option>
<option value="Melton">Melton</option>
<option value="Norris">Norris</option>
<option value="Tellico">Tellico</option>
<option value="Watts-Bar">Watts Bar</option>
</select>
*/?>
<span class="help-block"><?php echo $lake_err; ?></span>
</div>
<?php //Drop down example
$lakelist=mysqli_query($link,"SELECT * FROM lakes ORDER BY lake ASC");
echo "<select class='form-control' name='lake' id='lake' onchange=\"reload(this.form)\" title='lake you want'><option value='0'>Select Lake</option>";
while($clist=mysqli_fetch_array($lakelist))
{
echo "<option value='$clist[lake]'>$clist[lake]</option>"."<br/>";
}
echo "</select>";
mysqli_close($link);
?>
<select name="ndock" id="ndock" ></select>
<div class="form-group">
<label>How many days would you like?</label>
<select class="form-control" name="days">
<option value="">Select...</option>
<option value="1">1 Day </option>
<option value="2">2 Day </option>
<option value="3">3 Day </option>
<option value="Week">1 week </option>
</select>
<span class="help-block"><?php echo $days_err; ?></span>
</div>
<div class="form-group">
<label>What day would you like to start the rental on [Month/day/year]?</label>
<input width="50%" type="text" class="form-control" name="date">
<span class="help-block"><?php echo $date_err; ?></span>
</div>
<div class="form-group">
<label>Number of sport jet skis to rent?</label>
<select class="form-control" name="numberofsports">
<option value="0">0 </option>
<option value="1">1 </option>
<option value="2">2 </option>
<option value="3">3 </option>
<option value="4">4 </option>
</select>
<span class="help-block"><?php echo $numberofsports_err; ?></span>
</div>
<div class="form-group">
<label>Number of Deluxe jet skis to rent?</label>
<select class="form-control" name="numberofdeluxe">
<option value="0">0 </option>
<option value="1">1 </option>
<option value="2">2 </option>
</select>
<span class="help-block"><?php echo $numberofdeluxe_err; ?></span>
</div>
<div class="form-group">
<input type="hidden" name ="smallTube" value="no"/>
<input type="checkbox" name="smallTube" value="yes"/><label>Small Tube</label><br>
<span class="help-block"><?php echo $smallTube_err; ?></span>
</div>
<div class="form-group">
<input type="hidden" name ="largeTube" value="no"/>
<input type="checkbox" name="largeTube" value="yes"><label>Large Tube</label><br>
<span class="help-block"><?php echo $largeTube_err; ?></span>
</div>
<div class="form-group">
<input type="hidden" name ="wakeboard" value="no"/>
<input type="checkbox" name="wakeboard" value="yes"><label>WakeBoard</label><br>
<span class="help-block"><?php echo $wakeboard_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Submit">
<input type="reset" class="btn btn-default" value="Reset">
</div>
</form>
<p></p>
</div>
</div>
<div class="column right" style="background-color:#bbb;">
<div class="wrapper">
<h2><b>Lake Notifacation</b></h2>
<p> You can select to get emails when we are at your lake of choice! When we are at your
lake of choice you can rent Jet Skis by the hour. Only 60$ per hour. Sign up now by clicking on
"Sign up for lake notification". You can turn this feature on or off at anytime.</p>
<h2><b>Sign up for Discounts</b></h2>
<p>You can get really good discounts by signing up for discounts. It will make sure when ever a
discount is aviable , you recive it! We will not send spam, only good discounts for Jet Ski rentals!
Sign up now!</p>
</div>
</div>
</div>
</div>
</body>
<!--==============================footer=================================-->
<footer>
<div class="row">
<div class="large-12 columns">
<div class="row">
<div class="large-8 columns">
<ul class="social">
<li class="twitter"><a href="https://twitter.com/">Follow us on Twitter</a></li>
<li class="facebook"><a href="https://www.facebook.com/">Like us on Facebook</a></li>
<li class="google"><a href="https://plus.google.com/">Add us to your G+ Circles</a></li>
</ul>
<ul class="inline-list">
<li><a href="/blog.html">Blog</a></li>
<li><a href="/privacy_policy.html">Privacy Policy</a></li>
<li>Created by CustomSoftware</li>
</ul>
</div>
<div class="large-4 columns">
<p class="text-right">Copyright (c) 2018, Jet Skies R US</p>
</div
</div>
</div>
</div>
</footer>
<?php
//<script>
// $(function (){
// $('#bookingForm').bookingForm({
// ownerEmail: '#'
// });
// })
// $(function() {
// $('#bookingForm input, #bookingForm textarea').placeholder();
// });
//</script>
?>
</html>
答案 0 :(得分:1)
这是因为您在复选框之前使用了隐藏的输入,并检查它是否不为空。
尝试检查是否设置了输入名称。
<div class="form-group">
<input type="checkbox" name="smallTube" value="yes"/><label>Small Tube</label><br>
<span class="help-block"><?php echo $smallTube_err; ?></span>
</div>
<div class="form-group">
<input type="checkbox" name="largeTube" value="yes"><label>Large Tube</label><br>
<span class="help-block"><?php echo $largeTube_err; ?></span>
</div>
<div class="form-group">
<input type="checkbox" name="wakeboard" value="yes"><label>WakeBoard</label><br>
<span class="help-block"><?php echo $wakeboard_err; ?></span>
</div>
并且:
//add info here for the check boxes.
if ( !isset( $_POST[ "smallTube" ] ) ) {
$smallTube = "no";
} else {
$smallTube = "yes";
}
//add info here for the check boxes.
if ( !isset( $_POST[ "largeTube" ] ) ) {
$largeTube = "no";
} else {
$largeTube = "yes";
}
//add info here for the check boxes.
if ( !isset( $_POST[ "wakeboard" ] ) ) {
$wakeboard = "no";
} else {
$wakeboard = "yes";
}