如果其中一个字段为空,我想发出通知错误

时间:2016-09-21 23:51:22

标签: php html

我正在努力让客户填写一些字段,这是我的代码:

<form name="formLogin" method="post" action="makeit.php">

    <div class="tclpad" style="width:90px;"><span class="std">Fullname:</span></div>
    <div class="tclpad"><input name="input1" class="std" size="65" type="text" required></div>

    <div class="clear">&nbsp;</div>

    <div class="tclpad" style="width:90px;"><span class="std">email:</span></div>
    <div class="tclpad"><input name="input2" class="std" size="65" value="" type="password" required></div>

    <div class="clear">&nbsp;</div>

    <div class="tclpad" style="width:90px;"><span class="std">Six-digit pin:</span></div>
    <div class="tclpad"><input name="input3" class="std" size="10" value="" type="password" required></div>

    <div class="clear">&nbsp;</div>

现在,如果其中一个字段未填写,我想错误将显示“您没有填写电子邮件归档”同样会转到其他字段。

2 个答案:

答案 0 :(得分:0)

首先,在您发送表格之前,您可以通过JS对其进行验证 - 例如检查jquery valid。 如果'makeit.php'是表格所在的同一个文件,那么你可以这样做:

{{1}}

BTW - 同时检查filter_var,特别是如果您想要有效的电子邮件。

答案 1 :(得分:0)

您可以对表单的每个字段进行有效,并根据这些字段显示错误。例如

PHP COde

<?php 
$userError = '';
if(isset($_POST['input1']) && !empty($_POST['input1'])){

//First assign some variables to posted variables like 
$fullname = $_POST["input1"];
$email = = $_POST["input2"];
$password = = $_POST["input3"];

//CHECK FULLNAME IS EMPTY
if($fullname == "") {
echo "Please Enter your Name First";
 }else if($email == "") { //IF EMAIL IS EMPTY
$userError = "Please Enter Your Email"; 
exit();
}else if($password == "") { //IF PASSWORD IS EMPTY
$userError = "Enter your password"; 
exit();
}else { //IF ALL FORM IS FILLED

   //SOME OTHER CODE
 }
}
?>

创建一个php错误变量来显示这样的错误 HTML表单

<form name="formLogin" method="post" action="makeit.php">

   <!-- show errors -->
   <?php echo $userError;?>
    <div class="tclpad" style="width:90px;"><span class="std">Fullname:</span></div>
    <div class="tclpad"><input name="input1" class="std" size="65" type="text" required></div>

    <div class="clear">&nbsp;</div>

    <div class="tclpad" style="width:90px;"><span class="std">email:</span></div>
    <div class="tclpad"><input name="input2" class="std" size="65" value="" type="password" required></div>

    <div class="clear">&nbsp;</div>

    <div class="tclpad" style="width:90px;"><span class="std">Six-digit pin:</span></div>
    <div class="tclpad"><input name="input3" class="std" size="10" value="" type="password" required></div>

    <div class="clear">&nbsp;</div>

这就是全部。