我创建了一个php页面,如果<select>
中的下拉列表中没有选择任何值,它将显示错误。我无法为选择部分指定值。
我需要
我得到错误:
这是我的代码。
function checkSelect() {
$(document).ready(function () {
$("#contact-form").submit(function (e) {
var a=0;
if($( "#select1 option:selected" ).val()=='0')
{ a=1;
}
if(!a=="1")
{ if($( "#select2 option:selected" ).val()==' ')
{
alert('Select2 required fields is ignored... please check your form' );
}
}
else
{alert('select1 required fields is ignored... please check your form' );}
});
});}
<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
</head>
<body>
<div id="contact-form">
<form id="contact" class="demo" method="post" action="">
<fieldset>
<label for="select1"><span>select1 <span class="required">*</span></span>
<select id="select1" name="select1" class="option3" class="required">
<option value="0" disabled selected style="display: none;">....</option>
<option id="select1" value="option0">option0</option>
<option id="select1" value="option1">option1</option>
<option id="select1" value="option2">option2</option>
</select>
</label>
<label for="select2" style= "margin-left: 40px;" ><span style= "width: 70px;">select2<span class="required">*</span></span>
<select class="option3" name="select2" id="select2" class="required">
<option value="0" disabled selected style="display: none;">....</option>
<option value="option0">option0</option>
<option value="option1">option1</option>
</select>
</label>
<label style="margin-left: 37%;padding: 5px;">
<input type="submit" name="submit" class="button" id="submit" value="Submit" onclick="checkSelect(this);"></label>
</fieldset>
</form>
</div><!-- /end #contact-form -->
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO `inputs`(select1,select2)
VALUES ('".$_POST["select1"]."','".$_POST["select2"]."')";
if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>;";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>;";
}
$conn->close();
}
?>
</body>
</html>