PHP表单验证

时间:2014-01-21 04:35:16

标签: php html forms

我是PHP的新手,所以请保持温和!

我正在尝试使用错误消息/确认消息构建一个简单的PHP表单验证。当我提交表单时,它应该检查字段是否为空并显示相应的消息。但它一直给我一个错误,我不明白为什么:

解析错误:语法错误,第6行的process.php中的意外T_IF

这是进程php代码。

<form action="process.php" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
E-mail: <input type="text" name="email"><br>
<input type="hidden" name="submitted" value="1">
<input type="submit">
</form>

<?php
//If form was submitted

if ($_POST['submitted']==1) {
$errormsg = ""; //Initialize errors
    if ($_POST[fname]){
        $fname = $_POST[fname]; //If fname was entered
    }
    else{
        $errormsg = "Please enter first name";
    }
    if ($_POST[lname]){
        $lname = $_POST[lname]; //If lname was entered
    }
    else{
        if ($errormsg){ //If there is already an error, add next error
            $errormsg = $errormsg . ", last name";
            }
        else{
             $errormsg = "Please enter last name";
        }    
    }
    if ($_POST[email]){
        $email = $_POST[email]; //If email was entered
    }
    else{
        if ($errormsg){ //If there is already an error, add next error
            $errormsg = $errormsg . " & email";
        }else{
            $errormsg = "Please enter email";
        }
    }   
}
if ($errormsg){ //If any errors display them
    echo "<div class=\"box red\">$errormsg</div>";
}
//If all fields present
if ($fname && $lname && $email){
    //Do something
    echo "<div class=\"box green\">Form completed!</div>";
}
?>

6 个答案:

答案 0 :(得分:2)

您忘记在帖子数组中添加",这是导致错误$_POST[lname]更改为$_POST['lname'];的原因。将字符串传递给$_POST[];

if ($_POST["fname"]){
        $fname = $_POST[fname]; //If fname was entered
    }
    else{
        $errormsg = "Please enter first name";
    }
    if ($_POST["lname"]){
        $lname = $_POST[lname]; //If lname was entered
    }
    else{
        if ($errormsg){ //If there is already an error, add next error
            $errormsg = $errormsg . ", last name";
            }
        else{
             $errormsg = "Please enter last name";
        }    
    }
    if ($_POST["email"]){
        $email = $_POST["email"]; //If email was entered
    }

答案 1 :(得分:1)

对于$ _POST变量,使用语法$ _POST ['你的变量名']

我更正了您的代码,如下所示:

<form action="test.php" method="post">
First Name: <input type="text" name="fname"><br>
Last Name: <input type="text" name="lname"><br>
E-mail: <input type="text" name="email"><br>
<input type="hidden" name="submitted" value="1">
<input type="submit">
</form>

<?php
//If form was submitted

if ($_POST['submitted']==1) {
$errormsg = ""; //Initialize errors
    if ($_POST['fname']){
        $fname = $_POST['fname']; //If fname was entered
    }
    else{
        $errormsg = "Please enter first name";
    }
    if ($_POST['lname']){
        $lname = $_POST['lname']; //If lname was entered
    }
    else{
        if ($errormsg){ //If there is already an error, add next error
            $errormsg = $errormsg . ", last name";
            }
        else{
             $errormsg = "Please enter last name";
        }    
    }
    if ($_POST['email']){
        $email = $_POST['email']; //If email was entered
    }
    else{
        if ($errormsg){ //If there is already an error, add next error
            $errormsg = $errormsg . " & email";
        }else{
            $errormsg = "Please enter email";
        }
    }   
}
if ($errormsg){ //If any errors display them
    echo "<div class=\"box red\">$errormsg</div>";
}
//If all fields present
if ($fname && $lname && $email){
    //Do something
    echo "<div class=\"box green\">Form completed!</div>";
}
?>

答案 2 :(得分:1)

某些$ _POST变量缺少单引号,这可能是导致错误的原因。但是,一般来说,我已经提出了其他代码建议。 我重新构建了您的代码以使其更具可扩展性,并通过以下增强功能实现更好的实践:

  1. 表单值将记住其先前的值,并且不会在每个帖子上删除。
  2. 删除了“已提交”字段,并替换为if (!empty($_POST)) {}以确保已发布表单。
  3. 将错误消息移动到数组中。根据我的口味,这是更易于维护和可读的(想象有15个以上的字段需要测试)。
  4. 添加了validate()函数以在验证测试中运行。
  5. 删除了变量赋值($ fname = $ _POST ['fname']),因为除了验证之外没有使用它们,验证可以直接访问它们。
  6. 移动了主if语句中的所有测试。
  7. 希望这有帮助!

    <form action="process.php" method="post">
    First Name: <input type="text" name="fname" value="<?php echo isset($_POST['fname']) ? $_POST['fname'] : ''?>"><br>
    Last Name: <input type="text" name="lname" value="<?php echo isset($_POST['lname']) ? $_POST['lname'] : ''?>"><br>
    E-mail: <input type="text" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''?>"><br>
    <input type="submit">
    </form>
    
    <?php
    //If form was submitted
    
    if (!empty($_POST)) {
    
        $errors = array();
    
        if (empty($_POST['fname'])){
           $errors[] = 'First name must be entered.';
        }
    
        if (empty($_POST['lname'])){
            $errors[] = 'Last name must be entered.';
        }
    
        if (empty($_POST['email'])){
           $errors[] = 'Email address must be entered.';
        }
    
    
        if ($errors){ //If any errors display them
            $error_msg = implode('<br>',$errors);
            echo "<div class=\"box red\">$error_msg</div>";
        } 
        //If all fields present
        elseif (validate()){
            //Do something
            echo "<div class=\"box green\">Form completed and validated!</div>";
        }
    }
    
    function validate() {
        /*you can run all your validation methods here, such as check for length, regexp email verification, etc.*/
        $validated = false;
        if ($_POST['fname'] && $_POST['lname'] && $_POST['email']) { 
            $validated = true;
        }
        return $validated;
    }
    ?>
    

答案 3 :(得分:0)

这里的问题是缺少register globals启用(这在我看来是件好事)并且没有使用正确的字符串封装。

您需要将$_POST[word]的每个存在都更改为$_POST['word']。请注意'

答案 4 :(得分:0)

正如Ohgodwhy所说,

您需要将$_POST[word]的每个存在都更改为$_POST['word']。请注意'

为什么使用<input type="hidden" name="submitted" value="1">,这不是一个好习惯。更好用。

if($_SERVER['REQUEST_METHOD'] == "POST") 

答案 5 :(得分:-1)

我正在使用简单验证的东西类型。这是我的javascript代码:

var ck_name = /^[A-Za-z0-9 ]{3,50}$/;
var ck_email = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/;
var ck_mob = /^[0-9 ]{8,11}$/;

function validate(form){
var name = form.name.value;
var email = form.email.value;
var mob = form.mob.value;

var errors = [];
if (!ck_name.test(name)) 
{
errors[errors.length] = "Your valid Name .";
}
if (!ck_email.test(email)) 
{
errors[errors.length] = "Your must enter a valid email address.";
}
if (!ck_mob.test(mob)) 
{
errors[errors.length] = "Your valid Mobile Number.";
}

if (errors.length > 0) 
{
reportErrors(errors);
return false;
}
return true;
}

function reportErrors(errors)
{
var msg = "Please Enter Valide Data...\n";
for (var i = 0; i<errors.length; i++) 
{
var numError = i + 1;
msg += "\n" + numError + ". " + errors[i];
}
alert(msg);
}