PHP字段检查功能有什么问题

时间:2013-11-03 23:56:38

标签: php forms function field

我有这个功能,但它不起作用。

function Checkfields($fields){
    $ret = false;
    foreach($fields as $field) {
        if ($_POST[$field] == "" || !isset($_POST[$field])) {
            $ret = true;
        }
    }
    if ($ret) {
        $ret = "All fields are required.";
        header('Location: ?erro='. $erro);
    }
}

和我一样使用:

Checkfields(array("username","password"));

更新: 我已经尝试改为:

function Checkfields($fields){
    $ret = false;
    foreach($fields as $field) {
        if ($_POST[$field] == "" || !isset($_POST[$field])) {
            $ret = true;
        }
    }
    if ($ret) {
        $erro = "All fields are required.";
        header('Location: ?erro='. $erro);
    }
}

没有任何反应。

2 个答案:

答案 0 :(得分:0)

function Checkfields($fields){
        $ret = false;
        foreach($fields as $field) {
            if (!isset($_POST[$field])) {
                $ret = true;
            }
        }
        if ($ret) {
            $error = "All fields are required.";
            header('Location: ?erro='. $error);
        }
}

在变量POST的代码验证中存在两个错误..如果元素不存在则显示错误

当$ ret == true

时 你输了

标题

$ret = "All fields are required.";
        header('Location: ?erro='. $erro);

重定向到其他页面时不发送响应错误

答案 1 :(得分:0)

为什么你的代码没有重定向,可能是因为头函数调用

您可能需要像

一样打电话
header("Location:http://www.example.com/page.php?erro=$err")

将example.com/page.php替换为您希望其重定向到的页面。由于该位置基本上创建了302重定向,浏览器将被重定向到新的URL。所以你应该提供绝对路径。