表单成功提交后如何通知显示

时间:2019-09-07 05:41:42

标签: html forms notifications submit

我更改了我的网站模板。当我更改联系表单模板而不是提交表单成功通知时,我更改了联系表单,也使以前的联系表单正常工作,而不在提交按钮下显示。我检查了所有详细信息,但找不到任何错误。帮我这个表格。

表单代码

<form class="contact-form" id="contact-form" method="post" action="sendemail.php">
                            <div class="form-group tiple">
                                <input type="text" class="contact-box" name="name" value="" placeholder="Enter Your Name">
                            </div>
                            <div class="form-group tiple">
                                <input type="email" class="contact-box" name="email" value="" placeholder="Enter Your Email">
                            </div>
                            <div class="form-group tiple">
                                <input type="text" pattern="[6|7|8|9][0-9]{9}" class="contact-box" name="phone" value="" placeholder="10 Digit Mobile No.">
                            </div>
                            <div class="form-group">
                            <label>Subject</label>
                                <select name="subject" class="form-control">
                                    <option value="Demo Registraion">Demo Registraion</option>
                                    <option value="Contact Us">Contact Us</option>
                                    <option value="Feedback">Feedback</option>
                                    <option value="Other">Other</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <textarea name="message" class="contact-box" placeholder="Your Message" rows="10"></textarea>
                            </div>
                            <button type="submit" class="short-line margin-top-10">Submit now</button>
                        </form>

sendmail.php代码

<?php

// Define some constants
define( "RECIPIENT_NAME", "NAME" );
define( "RECIPIENT_EMAIL", "mail@email.com " );

// Read the form values
$success = false;
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$senderPhone = isset( $_POST['phone'] ) ? preg_replace( "/[6|7|8][0-9][9]/", "", $_POST['phone'] ) : "";
$subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

// If all values exist, send the email
if ( $senderName && $senderEmail && $message ) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $senderName . " <" . $senderEmail . ">";
  $mailBody = 'Sender Name: ' . $senderName. "\r\n" . 'Sender Email: ' . $senderEmail . "\r\n" .'Sender Phone: '. $senderPhone . "\r\n" . 'Subject: ' . $subject . "\r\n" . 'Message: ' . $message;
  $success = mail( $recipient, $subject, $mailBody, $headers );
  echo 'Your Request Send Successful';
  echo "<p class='success'>Thanks for contacting us. We will contact you ASAP!</p>";
?>

我要在“提交”按钮下发送通知 信息提交成功或失败

1 个答案:

答案 0 :(得分:0)

您可以遵循PRG模式并使用RedirectAttributes添加Flash属性。

例如:

@RequestMapping(value = "/contractor", method = RequestMethod.POST)
public String handle(@Valid @ModelAttribute Contractor contractor,
                     BindingResult result,
                     RedirectAttributes redirectAttributes) {

    // Save contactor ...

    redirectAttributes.addFlashAttribute("message", "Successful!");

    return "redirect:/someGetUrl";
}

只需在/ someGetUrl处理程序渲染的视图中显示此消息即可。