Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 表单在提交前进行验证_Php_Forms_Validation - Fatal编程技术网

Php 表单在提交前进行验证

Php 表单在提交前进行验证,php,forms,validation,Php,Forms,Validation,只是一个简单的例子,我似乎无法发现,希望其他人能发现我的错误,我有一个用PHP编写的字符串验证代码,我遇到的问题是,表单似乎在打开“CustomerName”字段时就验证了它,而不是在点击提交按钮时,表单一打开就验证了错误“请输入您的名字”甚至在按下提交按钮之前就显示出来了。有人能发现我的错误吗 <?php $flag = false; $badchar = ""; $string = $_POST["customerfname"]; $string = trim($string); $

只是一个简单的例子,我似乎无法发现,希望其他人能发现我的错误,我有一个用PHP编写的字符串验证代码,我遇到的问题是,表单似乎在打开“CustomerName”字段时就验证了它,而不是在点击提交按钮时,表单一打开就验证了错误“请输入您的名字”甚至在按下提交按钮之前就显示出来了。有人能发现我的错误吗

<?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>

您应该添加一个名为“submit”的提交按钮,并在您的
if(isset($\u 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>

学习regex,你不会后悔的。尝试过,没有问题……我注意到没有发送$_POST['submit'],因此甚至没有输入验证代码。你没有提交按钮吗?你应该将所有内容从isset($_POST['submit'])下的第5行移到第8行。你缺少一个带有“提交”的提交按钮“name那我该怎么写呢?如果$\u帖子符合你的要求,我会使用$valid,如果不符合你的要求,你应该这样写:复制整个代码,并在输入后添加它。我确信它可以工作。复制了确切的代码,并添加了代码以显示我的评论,但仍然存在相同的问题,不过,感谢您提供的提示。我确实有一个名为“提交”的提交按钮,目前整个PHP代码部分都在(isset($\u POST[“submit”])下,但我尝试了您的方法,但仍然没有成功。请尝试“if(empty($string))”而不是“if($length==0)”。顺便问一下,你的PHP版本是什么?
<?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>