php PHP中的弹出窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15659522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Popup window in PHP?
提问by CodeSigns
here's a quick question:
这是一个简单的问题:
How do you make a window pop up for your error handling in php? Is javascript the only way to do this or is there a way to do this in PHP?
你如何在 php 中为你的错误处理弹出一个窗口?javascript 是做到这一点的唯一方法还是有办法在 PHP 中做到这一点?
i have a PHP-file, and one of the lines of code is the following:
我有一个 PHP 文件,其中一行代码如下:
echo implode('<br />', $errors);
This line of code catches all the errors coming from my form, as following:
这行代码捕获了来自我的表单的所有错误,如下所示:
if (strlen($_POST['name'])<3) $errors[] = 'De naam van uw bedrijf moet minstens 3 letters bevatten';
if (strlen($_POST['number'])<9) $errors[] = 'Uw nummer moet minstens 9 nummers bevatten';
if (!preg_match($regexp_mail, $_POST['email'])) $errors[] = 'Onjuist E-mailadres';
when somebody presses submit on my form, the errors will show themselves on a separate page, i want this in a pop-up window. Thank you in advance
当有人在我的表单上按下提交时,错误将显示在单独的页面上,我希望在弹出窗口中显示。先感谢您
回答by jimcavoli
You'll have to use JS to open the popup, though you can put it on the page conditionally with PHP, you're right that you'll have to use a JavaScript function.
您必须使用 JS 来打开弹出窗口,尽管您可以使用 PHP 将其有条件地放在页面上,但您必须使用 JavaScript 函数是对的。
回答by NoLiver92
For a popup javascript is required. Put this in your header:
对于弹出的 javascript 是必需的。把它放在你的标题中:
<script>
function myFunction()
{
alert("I am an alert box!"); // this is the message in ""
}
</script>
And this in your body:
这在你的身体里:
<input type="button" onclick="myFunction()" value="Show alert box">
When the button is pressed a box pops up with the message set in the header.
当按下按钮时,会弹出一个框,其中包含在标题中设置的消息。
This can be put in any html or php file without the php tags.
这可以放在任何没有 php 标签的 html 或 php 文件中。
-----EDIT-----
- - -编辑 - - -
To display it using php try this:
要使用 php 显示它,试试这个:
<?php echo '<script>myfunction()</script>'; ?>
It may not be 100% correct but the principle is the same.
它可能不是 100% 正确,但原理是一样的。
To display different messages you can either create lots of functions or you can pass a variable in to the function when you call it.
要显示不同的消息,您可以创建大量函数,也可以在调用函数时将变量传递给该函数。
回答by Daryl Gill
if (isset($_POST['Register']))
{
$ErrorArrays = array (); //Empty array for input errors
$Input_Username = $_POST['Username'];
$Input_Password = $_POST['Password'];
$Input_Confirm = $_POST['ConfirmPass'];
$Input_Email = $_POST['Email'];
if (empty($Input_Username))
{
$ErrorArrays[] = "Username Is Empty";
}
if (empty($Input_Password))
{
$ErrorArrays[] = "Password Is Empty";
}
if ($Input_Password !== $Input_Confirm)
{
$ErrorArrays[] = "Passwords Do Not Match!";
}
if (!filter_var($Input_Email, FILTER_VALIDATE_EMAIL))
{
$ErrorArrays[] = "Incorrect Email Formatting";
}
if (count($ErrorArrays) == 0)
{
// No Errors
}
else
{
foreach ($ErrorArrays AS $Errors)
{
echo "<font color='red'><b>".$Errors."</font></b><br>";
}
}
}
?>
<form method="POST">
Username: <input type='text' name='Username'> <br>
Password: <input type='password' name='Password'><br>
Confirm Password: <input type='password' name='ConfirmPass'><br>
Email: <input type='text' name='Email'> <br><br>
<input type='submit' name='Register' value='Register'>
</form>
This is a very basic PHP Form validation. This could be put in a try
block, but for basic reference, I see this fit following our conversation in the comment box.
这是一个非常基本的 PHP 表单验证。这可以放在一个try
块中,但作为基本参考,我认为这符合我们在评论框中的对话。
What this script will do, is process each of the post elements, and act accordingly, for example:
此脚本将执行的操作是处理每个 post 元素,并相应地执行操作,例如:
if (!filter_var($Input_Email, FILTER_VALIDATE_EMAIL))
{
$ErrorArrays[] = "Incorrect Email Formatting";
}
This will check:
这将检查:
if $Input_Email is not a valid email. If this is not a valid E-mail, then a message will get added to a empty array.
如果 $Input_Email 不是有效的电子邮件。如果这不是有效的电子邮件,则会将一条消息添加到空数组中。
Further down the script, you will see:
在脚本的进一步下方,您将看到:
if (count($ErrorArrays) == 0)
{
// No Errors
}
else
{
foreach ($ErrorArrays AS $Errors)
{
echo "<font color='red'><b>".$Errors."</font></b><br>";
}
}
Basically. if the array count is not 0, errors have been found. Then the script will print out the errors.
基本上。如果数组计数不为 0,则已发现错误。然后脚本将打印出错误。
Remember, this is a reference based on our conversation in the comment box, and should be used as such.
请记住,这是基于我们在评论框中的对话的参考,应该如此使用。
回答by ComFreek
PHP runs on the server-side thus you have to use a client-side technology which is capable of showing popup windows: JavaScript.
PHP 在服务器端运行,因此您必须使用能够显示弹出窗口的客户端技术:JavaScript。
So you should output a specific JS block via PHP if your form contains errors and you want to show that popup.
因此,如果您的表单包含错误并且您想显示该弹出窗口,您应该通过 PHP 输出特定的 JS 块。