Php 拒绝空项或最小项的验证
当我只输入一个输入并提交时,它就会进入数据库。当我提交空表单时,它会在数据库中创建空间 我想要1)当用户将表单留空并提交时,拒绝插入2)当用户输入1或2个输入并提交时,拒绝提交Php 拒绝空项或最小项的验证,php,Php,当我只输入一个输入并提交时,它就会进入数据库。当我提交空表单时,它会在数据库中创建空间 我想要1)当用户将表单留空并提交时,拒绝插入2)当用户输入1或2个输入并提交时,拒绝提交 include 'database/dbconfig.php'; $insert =new connection($DB_con); $full_name = ""; $email = ""; $mobile= ""; $noc =""; $message = ""; $full_nameerror = ""; $ema
include 'database/dbconfig.php';
$insert =new connection($DB_con);
$full_name = "";
$email = "";
$mobile= "";
$noc ="";
$message = "";
$full_nameerror = "";
$email_error = "";
$mobile_error ="";
$noc_error ="";
$message_error="";
if(isset($_POST['submit']))
{
// checking null values in message
if (empty($_POST["full_name"])){
$full_nameerror = "Please enter your name";
}
else {
$full_name = test_input($_POST["full_name"]);
// check name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$full_name)){
$full_nameerror = "Only letters and white space allowed";
}
}
// checking null values in message
if (empty($_POST["email"])) {
$email_error = "Please enter your email";
}
else {
$email = test_input($_POST["email"]);
}
// checking null values in message
if (empty($_POST["mobile"])) {
$mobile_error = "Please enter your mobile number";
}
else {
$mobile = test_input($_POST["mobile"]);
}
if (empty($_POST["noc"])) {
$noc_error = "Nature of contact is required";
}
else {
$noc_error = test_input($_POST["noc"]);
}
// checking null values in message
if (empty($_POST["message"])) {
$message_error = "Please enter your message";
}
else {
$message = test_input($_POST["message"]);
}
if ($full_name|| $email || $mobile || $email || $noc || $message != "")
if ($insert->create($full_name,$email,$mobile,$noc,$message))
{
echo "Record inserted";
}
else
{
echo "error in executing query";
}
}
else
{
echo "Empty input submit2"; // empty $_POST["submit2"]
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form class="form-horizontal" method="post" id="contact-form">
<fieldset>
<!-- Form Name -->
<legend class="text-center" ><h2>Contact form</h2></legend>
<div id="error">
<!-- error will be shown here ! -->
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="full_name">Full Name</label>
<div class="col-md-5">
<input id="full_name" name="full_name" placeholder="Enter your full name" class="form-control input-md" type="text">
<span class="help-block" style="color:red;"><?php echo $full_nameerror;?></span>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="email">Email</label>
<div class="col-md-5">
<input id="email" name="email" placeholder="Enter your email" class="form-control input-md" type="text">
<span class="help-block" style="color:red;"><?php echo $email_error;?></span>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="mobile">Mobile(Optional)</label>
<div class="col-md-5">
<input id="mobile" name="mobile" placeholder="Enter your mobile number" class="form-control input-md" type="text">
<span class="help-block" style="color:red;"><?php echo $mobile_error;?></span>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="noc">Nature of Contact:</label>
<div class="col-md-5">
<select id="noc" name="noc" class="form-control">
<option value="">Select a Nature of Contact</option>
<option value="Request_for_a_service">Request for a service</option>
<option value="Report_an_issue">Report an issue</option>
<option value="Enquire_about_us">Enquire about us</option>
<option value="Work_with_us">Work with us</option>
<option value="Commend_us">Commend us</option>
<option value="Other_issue">Other issue</option>
</select>
</div>
</div>
<!-- Textarea -->
<div class="form-group">
<label class="col-md-4 control-label" for="message">Message</label>
<div class="col-md-4">
<textarea class="form-control" id="message" name="message"></textarea>
</div> <span class="help-block" style="color:red;"><?php echo $message_error;?></span>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="submit"></label>
<div class="col-md-4">
<button id="submit" name="submit" class="btn btn-success">Contact us</button>
包括“database/dbconfig.php”;
$insert=新连接($DB\U con);
$full_name=“”;
$email=“”;
$mobile=“”;
$noc=“”;
$message=“”;
$full_nameerror=“”;
$email_error=“”;
$mobile_error=“”;
$noc_error=“”;
$message_error=“”;
如果(isset($_POST['submit']))
{
//检查消息中的空值
如果(空($_POST[“全名”])){
$full_nameerror=“请输入您的姓名”;
}
否则{
$full_name=测试输入($_POST[“full_name”]);
//检查名称仅包含字母和空格
如果(!preg_match(“/^[a-zA-Z]*$/”,$full_name)){
$full_nameerror=“仅允许字母和空白”;
}
}
//检查消息中的空值
如果(空($_POST[“email”])){
$email\u error=“请输入您的电子邮件”;
}
否则{
$email=test_输入($_POST[“email”]);
}
//检查消息中的空值
如果(空($_POST[“mobile”])){
$mobile\u error=“请输入您的手机号码”;
}
否则{
$mobile=test_输入($_POST[“mobile”]);
}
如果(空($_POST[“noc”])){
$noc_error=“需要联系人的性质”;
}
否则{
$noc_error=测试_输入($_POST[“noc”]);
}
//检查消息中的空值
如果(空($_POST[“message”])){
$message\u error=“请输入您的消息”;
}
否则{
$message=test_输入($_POST[“message”]);
}
如果($full|u name | |$email | |$mobile | |$email | | |$noc | |$message!=“”)
如果($insert->create($full_name、$email、$mobile、$noc、$message))
{
回显“插入记录”;
}
其他的
{
echo“执行查询时出错”;
}
}
其他的
{
echo“Empty input submit2”;//Empty$\u POST[“submit2”]
}
功能测试输入($data){
$data=修剪($data);
$data=条带斜杠($data);
$data=htmlspecialchars($data);
返回$data;
}
?>
联系方式
全名
电子邮件
移动(可选)
接触性质:
选择联系人的性质
请求服务
报告问题
询问我们
与我们合作
赞扬我们
其他问题
消息
联系我们
这是我的课
<?php
class connection
{
private $db;
public $DB_con;
function __construct($DB_con)
{
$this->db = $DB_con;
}
public function create($full_name,$email,$mobile,$noc,$message)
{
try
{
$stmt = $this->db->prepare("INSERT INTO form(full_name,email,mobile,noc,message) VALUES(:fname, :email,:mobile,:noc,:message)");
$stmt->bindparam(":fname",$full_name);
$stmt->bindparam(":email",$email);
$stmt->bindparam(":mobile",$mobile);
$stmt->bindparam(":noc",$noc);
$stmt->bindparam(":message",$message);
$stmt->execute();
return $stmt;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
}
?>
如果($full|u name | | |$email | |$mobile | | |$email | |$noc | |$message!=”)
如您所说的,如果存在其中一个字段,则将其添加到表中empty()
之前进行修剪,因为empty(“”)==true
而empty(“”)==false
include 'database/dbconfig.php';
$conn = new connection($DB_con);
$full_name = "";
$email = "";
$mobile= "";
$noc ="";
$message = "";
// associative array to hold all your error messages;
// the keys are the names of the inputs and the values are the associated messages
$errors = array();
// trim POSTed values
$_POST = array_map('trim', $_POST);
// check if form was submitted
if (isset($_POST['submit'])) {
// validate fields
if (empty($_POST["full_name"])) {
$errors['full_name'] = "Please enter your name";
} elseif (!preg_match("/^[a-zA-Z ]*$/", $_POST["full_name"])) {
$errors['full_name'] = "Only letters and white space allowed";
} else {
$full_name = $_POST["full_name"];
}
if (empty($_POST["email"])) {
$errors['email'] = "Please enter your email";
} else {
$email = $_POST["email"];
}
if (empty($_POST["mobile"])) {
$errors['mobile'] = "Please enter your mobile number";
} else {
$mobile = $_POST["mobile"];
}
if (empty($_POST["noc"])) {
$errors['noc'] = "Nature of contact is required";
} else {
$noc = $_POST["noc"];
}
if (empty($_POST["message"])) {
$errors['message'] = "Please enter your message";
} else {
$message = $_POST["message"];
}
// check if there are any errors
if ($errors) {
echo "error in form";
// otherwise try adding a record
} elseif ($conn->create($full_name, $email, $mobile, $noc, $message)) {
echo "record inserted";
} else {
echo "error in inserting data";
}
}
要显示任何错误,您可以执行以下操作
<?php if (isset($errors['full_name'])) : ?>
<span class="help-block" style="color:red;"><?= $errors['full_name'] ?></span>
<?php endif ?>
如果($full|u name | | |$email | |$mobile | | |$email | |$noc | |$message!=”)
如您所说的,如果存在其中一个字段,则将其添加到表中empty()
之前进行修剪,因为empty(“”)==true
而empty(“”)==false
include 'database/dbconfig.php';
$conn = new connection($DB_con);
$full_name = "";
$email = "";
$mobile= "";
$noc ="";
$message = "";
// associative array to hold all your error messages;
// the keys are the names of the inputs and the values are the associated messages
$errors = array();
// trim POSTed values
$_POST = array_map('trim', $_POST);
// check if form was submitted
if (isset($_POST['submit'])) {
// validate fields
if (empty($_POST["full_name"])) {
$errors['full_name'] = "Please enter your name";
} elseif (!preg_match("/^[a-zA-Z ]*$/", $_POST["full_name"])) {
$errors['full_name'] = "Only letters and white space allowed";
} else {
$full_name = $_POST["full_name"];
}
if (empty($_POST["email"])) {
$errors['email'] = "Please enter your email";
} else {
$email = $_POST["email"];
}
if (empty($_POST["mobile"])) {
$errors['mobile'] = "Please enter your mobile number";
} else {
$mobile = $_POST["mobile"];
}
if (empty($_POST["noc"])) {
$errors['noc'] = "Nature of contact is required";
} else {
$noc = $_POST["noc"];
}
if (empty($_POST["message"])) {
$errors['message'] = "Please enter your message";
} else {
$message = $_POST["message"];
}
// check if there are any errors
if ($errors) {
echo "error in form";
// otherwise try adding a record
} elseif ($conn->create($full_name, $email, $mobile, $noc, $message)) {
echo "record inserted";
} else {
echo "error in inserting data";
}
}
要显示任何错误,您可以执行以下操作
<?php if (isset($errors['full_name'])) : ?>
<span class="help-block" style="color:red;"><?= $errors['full_name'] ?></span>
<?php endif ?>
帮助大师帮助我帮助大师帮助我