想象我有一个错误数组来包含注册表单中的任何错误,我有一个名为output_errors()
的函数,其参数是errors数组。
有没有办法自定义弹出窗口以显示显示错误的功能?
<?php
$error = array();
function output_errors($errors) {
return '<ul><li>' . implode('</li><li>',$errors) . '</li></ul>';
}
if (user_exists($_POST['username']) === true) {
$errors[] = 'Sorry, the username \'' . $_POST['username'] . '\' is already taken.';
}
if (preg_match("/\\s/", $_POST['username']) == true) {
$errors[] = 'Your username must not contain any spaces.';
}
if (strlen($_POST['password']) < 6) {
$errors[] = 'Your password must be at least 6 characters';
}
if ($_POST['password'] !== $_POST['password_again']) {
$errors[] = 'Your passwords do not match';
}
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
$errors[] = 'A valid email address is required';
}
if (email_exists($_POST['email']) === true) {
$errors[] = 'Sorry, email \'' . $_POST['email'] . '\' exists.';
}
if (empty($_POST) === false && empty($errors) === true) {
$register_data = array(
'username' => $_POST['username'],
'password' => $_POST['password'],
'first_name' => $_POST['first_name'],
'last_name' => $_POST['last_name'],
'email' => $_POST['email']
);
register_user($register_data);
header('Location: register.php?success');
exit();
} else if(empty($errors) === false) {
echo output_errors($errors);
?>
<form action="" method="post">
<ul>
<li>
<i>*All fields marked with asterisk are required!</i><br>
<input type="text" placeholder="Username" name="username">
</li>
<li>
<br>
<input type="password" placeholder="*Password" name="password">
</li>
<li>
br>
<input type="password" placeholder="*Retype Password" name="password_a">
</li>
<li>
<br>
<input type="text" placeholder="*First name" name="first_name">
</li>
<li>
<br>
<input type="text" placeholder="Last name" name="last_name">
</li>
<li>
<br>
<input type="text" placeholder="*Email" name="email">
</li>
<li>
<input type="submit" value="Register">
</li>
</ul>
</form>
答案 0 :(得分:0)
您无法自定义JavaScript的默认弹出窗口,您可以使用Bootstrap或执行以下操作:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test!</button>
<script>
function myFunction() {
var myWindow = window.open("", "MsgWindow", "width=200, height=100");
myWindow.document.write("<p>This is 'MsgWindow'. I am 200px wide and 100px tall!</p>");
}
</script>
</body>
</html>
在document.write(content)
中,此处的内容将是您的错误消息。
您可以通过执行以下操作在js中插入php:
document.write(<?php echo "Text"; ?>);
看看是否有帮助。
答案 1 :(得分:-1)
我不确定这是否是您正在寻找的内容,但如果您在最后一行替换echo语句:
echo "<script>";
echo "window.open('', 'MsgWindow', 'width=200, height=100').document.write('" . output_errors($errors) . "');";
echo "</script>";
您将看到一个弹出窗口,其中显示错误。也就是说,代码中存在一些错误,可以立即显示错误,特别是密码字段。所以上面的内容会有效,但是一旦页面加载,你就会有一个弹出窗口。