表格在提交前确认

时间:2013-05-17 07:58:17

标签: php forms validation

只是一个快速的,我似乎无法发现,并希望其他人可以发现我的错误,我有一些用PHP编写的字符串验证代码,我遇到的问题是表单似乎验证了“customerfname”字段一旦打开就不会在按下提交按钮时,一旦打开表单,即使在按下提交按钮之前,也会显示错误“请输入您的名字”。有谁能发现我的错误?

<?php

$flag = false;
$badchar = "";
$string = $_POST["customerfname"];
$string = trim($string);
$length = strlen($string);
$strmsg = "";

if (isset($_POST["submit"])) {
    if ($length == 0) {
        $strmsg = '<span class="error"> Please enter your first name</span>';
        $flag = true;
        $valid = false;}
    else if ($length > 30) {
        $strmsg = '<span class="error"> Can not enter more than 30 characters</span>';
        $flag = true;
        $valid = false;
        }
else{
    for ($i=0; $i<$length;$i++){
        $c = strtolower(substr($string, $i, 1));
        if (strpos("abcdefghijklmnopqrstuvwxyz-", $c) === false){
             $badchar .=$c;
             $flag = true;
             $valid = false;
        }
    }
    if ($flag) {
        $strmsg = '<span class="error"> The field contained the following invalid characters: '.$badchar.'</span>';}
    }
    if (!$flag) {
        $valid = true;
    }
}

?>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="custinfo">

<td><label for="customerfname">Customer First Name: </label></td>
<td><input type="text" id="customerfname" name="customerfname" size=50/><?php echo $strmsg; ?></td>

<p><input type="submit" name="submit" value="Save Data"/>&nbsp;<input type="reset" value="Clear Form" />

</form>

2 个答案:

答案 0 :(得分:0)

<?php
$flag = false;
$badchar = "";
$strmsg = "";
if(isset($_POST['customerfname']))
{
$string = $_POST["customerfname"];
$string = trim($string);
$length = strlen($string);
}
if (isset($_POST["submit"])) {
if ($length == 0) {
$strmsg = '<span class="error"> Please enter your first name</span>';
$flag = true;
$valid = false;}
else if ($length > 30) {
$strmsg = '<span class="error"> Can not enter more than 30 characters</span>';
$flag = true;
$valid = false;}
else {
for ($i=0; $i<$length;$i++){
    $c = strtolower(substr($string, $i, 1));
    if (strpos("abcdefghijklmnopqrstuvwxyz-", $c) === false){
        $badchar .=$c;
        $flag = true;
        $valid = false;
    }
}
if ($flag) {
    $strmsg = '<span class="error"> The field contained the following invalid characters: '.$badchar.'</span>';}
}
if (!$flag) {
    $valid = true;}
    var_dump($valid);
}

?>

<form method="POST" action="" id="custinfo">

<td><label for="customerfname">Customer First Name: </label></td>
<td><input type="text" id="customerfname" name="customerfname" size=50/><?php echo $strmsg; ?></td>
<td><input type="submit" name="submit" /></td>

</form>

答案 1 :(得分:0)

您应该添加一个“提交”作为名称的提交按钮,并在if (isset($_POST["submit"]))

下移动有关POST请求的所有行

以下是您的代码的工作(还有很多需要改进的,但这是一个开头):

<?php

$flag = false;
$badchar = "";
$strmsg = "";

if (isset($_POST["submit"])) {

    $string = (isset($_POST["customerfname"])) ? $_POST["customerfname"] : ''; // if customerfname wasn't sent, $string will be empty
    $string = trim($string);
    $length = strlen($string);

    if ($length == 0) {
        $strmsg = '<span class="error"> Please enter your first name</span>';
        $flag = true;
        $valid = false;
    }
    else if ($length > 30) {
        $strmsg = '<span class="error"> Can not enter more than 30 characters</span>';
        $flag = true;
        $valid = false;
    }
    else {
        for ($i=0; $i<$length;$i++) {
                $c = strtolower(substr($string, $i, 1));
                if (strpos("abcdefghijklmnopqrstuvwxyz-", $c) === false) {
                    $badchar .=$c;
                    $flag = true;
                    $valid = false;
                }
        }
        if ($flag) {
            $strmsg = '<span class="error"> The field contained the following invalid characters: '.$badchar.'</span>';
        }
    }
    if (!$flag) {
        $valid = true;
    }
}

?>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="custinfo">

<td><label for="customerfname">Customer First Name: </label></td>
<td><input type="text" id="customerfname" name="customerfname" size=50/><?php echo $strmsg; ?></td>
<td><input type="submit" name="submit"></td>

</form>