php 提交表单后如何获得弹出框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18126665/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:02:10  来源:igfitidea点击:

How to get a popup box after submitting a form?

phpjavascripthtml

提问by Swobbleton

[SOLVED]Thanks everyone that replied.

[已解决]谢谢大家回复。

I've got a PHP script that is run after a user submited a contact form. This script shows a message in the top left corner saying if it worked properly or if it encountered a problem.

我有一个在用户提交联系表单后运行的 PHP 脚本。此脚本在左上角显示一条消息,说明它是否正常工作或是否遇到问题。

Now my question is, how can I put those messages into a popup box? I know there is JS involved, but I barely have knowledge of that.

现在我的问题是,如何将这些消息放入弹出框中?我知道有 JS 参与,但我几乎不知道。

( Example: http://www.dylanvanheugten.nl/contact.php)

(例如:http: //www.dylanvanheugten.nl/contact.php



PHP

PHP

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Site Contact';
$to = '[email protected]';
$subject = $_POST['subject'];
$human = $_POST['human'];

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

if ($_POST['submit']) {
    if ($name != '' && $email != '' && $subject != '' && $message != '') {
        if ($human == '12') {                
            if (mail ($to, $subject, $body, $from)) { 
                echo '<p>Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug.</p>';
            } else { 
                echo '<p>Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.</p>'; 
            } 
        } else if ($_POST['submit'] && $human != '12') {
            echo '<p>U heeft de spam preventie som foutief beantwoord.</p>';
        }
    } else {
        echo '<p>Alstublieft alle velden invullen.</p>';
    }
}


HTML

HTML

<form method="post" action="contact.php">
    <label>Naam</label>
    <input name="name" placeholder="Naam">

    <label>Email</label>
    <input name="email" type="email" placeholder="Email">

    <label>Onderwerp</label>
    <input name="subject" placeholder="Onderwerp">

    <label>Bericht</label>
    <textarea name="message" placeholder="Laat hier ook uw telefoonnummer achter als u telefonisch contact wilt."></textarea>

    <label><strong>*Spam preventie*</strong><br/>Wat is 11+1?</label>
    <input name="human" placeholder="11 + 1 =">

    <input id="submit" name="submit" type="submit" value="Verzend">
    <label><u>Alle velden zijn verplicht.</u></label>
</form>

采纳答案by Shankar Damodaran

Enclose your echowith <script>tags like this.

echo用这样的<script>标签附上你的标签。

echo '<script>alert("Alstublieft alle velden invullen");<script>';

回答by Connor

In the same way that you can print HTML from PHP, you can 'print' javascript. Here's an example with an alert box:

就像您可以从 PHP 打印 HTML 一样,您可以“打印”javascript。这是一个带有警告框的示例:

print("<script>window.alert('This is a javascript alert from PHP');</script>");

You are able to put your PHP variables inside of the statement, as well, if you need to.

如果需要,您也可以将 PHP 变量放在语句中。

回答by Rohan Kumar

You can simply alert msglike,

你可以简单地alert msg喜欢,

$msg='';
if ($_POST['submit']) {
    if ($name != '' && $email != '' && $subject != '' && $message != '') {
        if ($human == '12') {                
            if (mail ($to, $subject, $body, $from)) { 
                $msg='Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug';
            } else { 
                $msg='Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.'; 
            } 
        } else if ($_POST['submit'] && $human != '12') {
            $msg='U heeft de spam preventie som foutief beantwoord.';
        }
    } else {
        $msg='Alstublieft alle velden invullen.';
    }
    echo '<script>
             alert("'.$msg.'");
          </script>';
    // you can replace the above javscript code to any plugin like jquery ui modal box
}

回答by Mentatmatt

Add some javascript to the top of your html markup where you define a method to display a popup. Something like this:

在 html 标记的顶部添加一些 javascript,您可以在其中定义显示弹出窗口的方法。像这样的东西:

<script type="text/javascript">
function displayPopup()
{
 alert("Form submitted!");
}
</script>

Then within your PHP when the form has been submitted, just call the method displayPopup()method within a script tag.

然后在您的 PHP 中提交表单后,只需在脚本标记中调用displayPopup()方法即可。

You can certainly optimise the approach above, but it should give you somewhere to start.

你当然可以优化上面的方法,但它应该给你一个开始的地方。

回答by Mentatmatt

Store the error message in a variable and simply echo them in the required div.

将错误消息存储在一个变量中,并在所需的 div 中简单地回显它们。

if ($_POST['submit']) 
{
    if ($name != '' && $email != '' && $subject != '' && $message != '') 
    {
        if ($human == '12') 
        {                
            if (mail ($to, $subject, $body, $from)) 
            { 
                $message = '<p>Uw bericht is verzonden.<br/> U krijgt binnen 3 werkdagen een bericht terug.</p>';
            } else 
            { 
                $message = '<p>Er is iets mis gegaan tijdens het versturen van uw bericht.<br/> Probeert u alstublieft nogmaals.</p>'; 
            } 
        } else if ($_POST['submit'] && $human != '12') 
        {
            $message =  '<p>U heeft de spam preventie som foutief beantwoord.</p>';
        }
    } 
    else 
    {
        $message = '<p>Alstublieft alle velden invullen.</p>';
    }
}

And in your HTML, you can create a and display the error inside it:

在您的 HTML 中,您可以创建一个并在其中显示错误:

<div id="errorMessage"> <?php echo $message; ?> </div>