简体   繁体   English

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

[英]What's wrong PHP field check function

I have that function but it's not working. 我有该功能,但无法正常工作。

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);
    }
}

and i use like that: 我这样使用:

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

UPDATED: I already tried to change to: 更新:我已经尝试更改为:

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);
    }
}

and nothing happens. 并没有任何反应。

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);
        }
}

Exist two bugs in your code validation in variable POST.. if element don't exist appears error 在变量POST中的代码验证中存在两个错误。如果元素不存在,则会出现错误

And when $ret == true 当$ ret == true时

header you put 您放置的标头

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

When redirect to other page don't send response error 重定向到其他页面时不发送响应错误

Why your code is not redirecting , is probably because of the header function call 为什么您的代码不重定向,可能是由于标头函数调用

you may have to call like 您可能必须打电话给

header("Location:http://www.example.com/page.php?erro=$err")

replace example.com/page.php to the page you want it to redirect to. 将example.com/page.php替换为您要重定向到的页面。 As the location basically creates a 302 redirection and the browser will get redirected to the new url. 由于该位置基本上会创建302重定向,因此浏览器将被重定向到新的url。 So you should supply absolute path. 因此,您应该提供绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM