0

只是一个快速的,我似乎无法发现并希望其他人能发现我的错误,我有一些用 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>
4

2 回答 2

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>
于 2013-05-17T08:09:20.373 回答
0

您应该添加一个以“提交”为名称的提交按钮,并将有关 POST 请求的所有行移到您的if (isset($_POST["submit"]))

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

<?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>
于 2013-05-17T08:15:07.793 回答