【问题标题】:How to display a message after form has successfully submitted表单提交成功后如何显示消息
【发布时间】:2014-02-17 17:43:56
【问题描述】:

我有一个用 PHP 处理的 HTML 表单。我想显示一条消息,让用户知道表单已成功提交。现在我不介意页面是否重新加载。我只想要一个“成功!”提交表单后出现的消息。

任何帮助都会很棒!

PHP

<?php
$errors = array();
$missing = array();
if (isset($_POST['submit'])) {
    $to = 'celinehalpin@hotmail.com';
    $subject = 'Web Design';
    $expected = array('name', 'company', 'email', 'phone', 'contactYou', 'interest', 'budget', 'comments');
    $required = array('name', 'email', 'contactYou', 'interest', 'budget', 'comments');
    $headers = "From: webmaster@example.com\r\n";
    $headers .= "Content-type: text/plain; charset=utf-8";
    $authenticate = '-fcelinehalpin@hotmail.com';
    require './_includes/mail_process.php';
    if ($mailSent) {
        header('Location: index.php#c');
        exit;   
    }
}
?>

HTML

<div id="contactBox">

            <form name="contact" id="contactForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                <div id="formLeft">
                    <p>
                        <label for="name">Name</label>
                        <?php if ($missing && in_array('name', $missing)) { ?>
                        <div class="warningDivLeft">
                        <span class="warning">Please enter your name</span>
                        </div>                             
                        <?php } ?>
                        <input type="text" name="name" id="name" tabindex="10"
                        <?php
                        if ($errors || $missing) {
                            echo 'value="' . htmlentities($name, ENT_COMPAT, 'utf-8') . '"';
                        }
                        ?>      
                        >
                    </p>
                    <p>                           
                        <label for="company">Company Name/Website</label>
                        <input type="text" name="company" id="company" tabindex="30"
                        <?php
                        if ($errors || $missing) {
                            echo 'value="' . htmlentities($company, ENT_COMPAT, 'utf-8') . '"';
                        }
                        ?>      
                        >
                    </p>
                    <p>                           
                        <label for="email">Email</label>
                        <?php if ($missing && in_array('email', $missing)) { ?>
                        <div class="warningDivLeft">
                        <span class="warning">Please enter your email</span>
                        </div>
                        <?php } elseif (isset($errors['email'])) { ?>
                        <div class="warningDivLeft">
                        <span class="warning">Invalid email address</span>
                        </div>
                        <?php } ?>
                        <input type="email" name="email" id="email" tabindex="40"
                        <?php
                        if ($errors || $missing) {
                            echo 'value="' . htmlentities($email, ENT_COMPAT, 'utf-8') . '"';
                        }
                        ?>      
                        >
                    </p>
                    <p>                           
                        <label for="phone">Phone</label>
                        <input type="text" name="phone" id="phone" tabindex="50"
                        <?php
                        if ($errors || $missing) {
                            echo 'value="' . htmlentities($phone, ENT_COMPAT, 'utf-8') . '"';
                        }
                        ?>      
                        >
                    </p>
                    <p>                           
                        <label for="contactYou">Contact you by...</label>
                        <?php if ($missing && in_array('contactYou', $missing)) { ?>
                        <div class="warningDivLeft">
                        <span class="warning">Please select one</span>
                        </div>
                        <?php } ?>
                        <select name="contactYou" size="1" id="contactYou" tabindex="60">
                            <option value="" selected="selected">- select</option>
                            <option value="email" <?php echo ($contactYou == 'email') ? ' selected="selected"' : ''; ?>>Email</option>
                            <option value="phone" <?php echo ($contactYou == 'phone') ? ' selected="selected"' : ''; ?>>Phone</option>
                        </select>
                    </p> 
                </div>
                <div id="formRight">
                    <p>
                        <label for="interest">I am interested in...</label>
                        <?php if ($missing && in_array('interest', $missing)) { ?>
                        <div class="warningDiv">
                        <span class="warning">Please select one</span>
                        </div>
                        <?php } ?>
                        <select name="interest" size="1" id="interest" tabindex="80">
                            <option value="" selected="selected">- select</option>
                            <option value="new" <?php echo ($interest == 'new') ? ' selected="selected"' : ''; ?>>Creating a new website</option>
                            <option value="current" <?php echo ($interest == 'current') ? ' selected="selected"' : ''; ?>>Redesigning a current website</option>
                            <option value="responsive" <?php echo ($interest == 'responsive') ? ' selected="selected"' : ''; ?>>Reponsive web design</option>
                            <option value="wordpress" <?php echo ($interest == 'wordpress') ? ' selected="selected"' : ''; ?>>A WordPress website</option>
                            <option value="general" <?php echo ($interest == 'general') ? ' selected="selected"' : ''; ?>>General enquiry</option>
                        </select>
                    </p>
                    <p>
                        <label for="budget">My budget is...</label>
                        <?php if ($missing && in_array('budget', $missing)) { ?>
                        <div class="warningDiv">
                        <span class="warning">Please select one</span>
                        </div>
                        <?php } ?>
                        <select name="budget" size="1" id="budget" tabindex="90">
                            <option value="" selected="selected">- select</option>
                            <option value="100" <?php echo ($budget == '100') ? ' selected="selected"' : ''; ?>>€100 - €500</option>
                            <option value="500" <?php echo ($budget == '500') ? ' selected="selected"' : ''; ?>>€500 - €1,000</option>
                            <option value="1000" <?php echo ($budget == '1000') ? ' selected="selected"' : ''; ?>>€1,000 - €2,000</option>
                            <option value="2000" <?php echo ($budget == '2000') ? ' selected="selected"' : ''; ?>>€2,000 - €5,000</option>
                            <option value="5000" <?php echo ($budget == '5000') ? ' selected="selected"' : ''; ?>>€5,000 - €10,000</option>
                            <option value="10000" <?php echo ($budget == '10000') ? ' selected="selected"' : ''; ?>>€10,000+</option>
                        </select>
                    </p>
                    <p>
                        <label for="comments">How can I help you?</label>
                        <?php if ($missing && in_array('comments', $missing)) { ?>
                        <div class="warningDiv">
                        <span class="warning">Please leave a comment</span>
                        </div>
                        <?php } ?>
                        <textarea name="comments" id="comments" cols="45" rows="5" tabindex="100"><?php
                        if ($errors || $missing) {
                            echo htmlentities($comments, ENT_COMPAT, 'utf-8');
                        }
                        ?></textarea>
                    </p>
                </div>
                <div id="formSubmit">
                    <ul>
                        <li>
                          <input type="submit" name="submit" id="submit" value="Send Message" tabindex="70">
                        </li>
                    </ul>
                </div>
                    </form>
        </div>

mail_process.php

<?php
$suspect = false;
$pattern = '/Content-Type:|Bcc:|Cc:/i';

function isSuspect($val, $pattern, &$suspect) {
    if (is_array($val)) {
        foreach ($val as $item) {
            isSuspect($item, $pattern, $suspect);   
        }
    } else {
        if (preg_match($pattern, $val)) {
            $suspect = true;    
        }
    }
}

isSuspect($_POST, $pattern, $suspect);

if (!$suspect) {
    foreach ($_POST as $key => $value) {
        $temp = is_array($value) ? $value : trim($value);
        if (empty($temp) && in_array($key, $required)) {
            $missing[] = $key;
            $$key = '';
        } elseif(in_array($key, $expected)) {
            $$key = $temp;
        }
    }
}

if (!$suspect && !empty($email)) {
    $validemail = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
    if ($validemail) {
        $headers .= "\r\nReply-to: $validemail";
    } else {
        $errors['email'] = true;
    }
}

if (!$suspect && !$missing && !$errors) {
    $message = '';
    foreach ($expected as $item) {
        if (isset($$item) && !empty($$item)) {
            $val = $$item;
        } else {
            $val = 'Not selected';
        }
        if (is_array($val)) {
            $val = implode(', ', $val); 
        }
        $item = str_replace(array('_', '-'), ' ', $item);
        $message .= ucfirst($item) . ": $val\r\n\r\n";
    }
    $message = wordwrap($message, 70);

    $mailSent = mail($to, $subject, $message, $headers, $authenticate);
    if (!$mailSent) {
        $errors['mailfail'] = true;
    }
}

【问题讨论】:

  • 好吧,您可以显示一条消息,而不是重定向到另一个页面。
  • 感谢您的评论。我该怎么做呢?

标签: php html forms


【解决方案1】:

代替:

header('Location: index.php#c');

前往:

header('Location: ' . $_SERVER['REQUEST_URI'] . '?success=1');

然后添加:

if (isset($_GET['success'])) print 'Message sent!';

任何你想要的地方。

【讨论】:

  • 谢谢!这行得通!但是现在页面重新加载到页面顶部。我的站点是一个单页站点,单击每个菜单项会将您带到页面的不同部分。我的联系表格在最底部。一旦页面重新加载,我将如何返回到相同的联系部分,或者更好的是,只是让消息出现而不重新加载?谢谢。
  • # 运算符告诉网页向下滚动到该 ID。所以你实际上可以输入header('Location: ' . $_SERVER['REQUEST_URI'] . '?success=1#c');,刷新后页面会向下滚动到该部分。然后在该部分中,您可以打印成功消息。另一种选择是不使用标头重定向,而只打印一条消息,例如die('Thank you, your message has been sent. Press BACK to return');
  • 非常感谢!这非常有效。我还有一个问题。当表单提交时出现错误(例如必填字段为空)时,页面会重新加载到页面顶部,而不是重新加载到页面的同一部分,以便用户可以看到错误消息。我认为它以前是这样工作的,但是新代码可能已经改变了一些事情。谢谢。
【解决方案2】:

$_GET 有效,但如果他们重新访问其历史记录中的链接(单击他们的历史记录、返回浏览器或刷新),它将始终存在

您可以在成功时设置$_SESSION,然后重定向到另一个页面(例如thanks.php)。

如果设置了会话,在thanks.php 中,以及您希望的任何标准,将它们回显出与会话中的数据相关的任何消息。

然后取消设置会话。

这样,如果他们点击链接并返回thanks.php页面,您的脚本将显示会话未设置,因此可以生成一般消息:

此页面用于确认发送内容。你就像你一样在这里 刷新页面或使用浏览器的后退按钮。作为...的一部分 安全程序,我们在发送并确认后清除数据等

刷新thanks.php,弄乱GET 数据等,等等,都无济于事。会话未设置,他们会收到友好的通用消息。

【讨论】: