Username: -6ren">
gpt4 book ai didi

PHP 表单验证在提交前显示错误

转载 作者:行者123 更新时间:2023-11-29 05:23:55 26 4
gpt4 key购买 nike

我正在制作一个 PHP 和 MySQL 应用程序来练习我对编程的新爱好。我有一个登录脚本; HTML 格式如下:

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

Username:

<input type="text" name="username" maxlength="60"><br />



Password:

<input type="password" name="password" maxlength="60"><br />



Confirm Password:

<input type="password" name="password_confirm" maxlength="60"><br />


<input type="submit" name="submit" value="Register"><br />

</form>

这继续进入 PHP 表单验证器:

<?php

$username_original = $_POST["username"];
$password_original = $_POST["password"];
$password_confirm_original = $_POST["password_confirm"];

//This makes sure they did not leave any fields blank

function FieldComplete($user, $password, $password_confirm) {
if (!isset($user, $password, $password_confirm) &&
$user && $password && $password_confirm == "" || " ") {
$field_confirm = false;
} else {
$field_confirm = true;
}


if ($field_confirm == true) {
echo "";
} else {
echo "You did not complete all the required fields.";
}
}

FieldComplete($username_original, $password_original, $password_confirm_original);

?>

我意识到为此创建一个函数可能看起来有点无用,但它已经接近我想要的结果了。

但是,此代码显示错误“您没有完成所有必填字段。”在加载页面时。我只希望在他们按下注册按钮但仍然有空白字段时显示该错误。

非常感谢任何建议或帮助。感谢 StackOverflow 社区!

最佳答案

如果你只想在提交按钮时运行 php 代码,那么你需要在页面顶部检查它

if(isset($_POST["submit"]))
{
//form has been submitted
//do validation and database operation and whatever you need
} else {
//form has not been submitted
//print the form
}

关于PHP 表单验证在提交前显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22466138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com