【问题标题】:Open Modal from PHP redirection (sending email)从 PHP 重定向打开 Modal(发送电子邮件)
【发布时间】:2021-04-03 11:19:39
【问题描述】:

我的目标是在成功将报价单发送到电子邮件后调用“谢谢”模式消息,但我找不到正确的代码。这就是页面的工作方式。我将填写表格,然后在提交后,它将加载 form.php,其中包含将用户的电子邮件发送到应该发送消息的指定电子邮件的代码。之后,form.php 会确认邮件是否发送成功。我的问题是,假设消息已发送,我将如何在“谢谢”模式打开的情况下重定向到主 index.html?

这是我的 index.html 代码。

<form class="login-form" method="post" action="php/form.php" name="quote-form" role="form">
  <fieldset>
    <div class="form-group">
      <input type="text" name="name" class="form-control" placeholder="Your Name" required data-error="Your Name is required.">
    </div>
    <div class="form-group">
      <input type="text" name="phone_number" class="form-control" placeholder="Phone Number " required data-error="Phone Number is required.">
    </div>

    <div class="form-group">
      <input type="email" name="email" class="form-control" placeholder="Email Address" required data-error="Valid email is required.">
    </div>

    <! --Adding Radio button For Call Back -->
    <div class="form-group">
      <label>Request Phone Call?: <br>
        <input type="radio" value ="Yes " required data-error="required." name="call">
        <em>Yes</em><br>
        <input type="radio" value ="No" required data-error="required." name="call">
        <em>No</em><br>
      </label>
    </div>

    <div class="form-group">
      <textarea id="textinput" class="form-control" name="message" required rows="1" placeholder="Please provide the ff: &#10; -Location &#10; -Dimension (Total Area less openings, All side running meters for endcap computation) &#10; -Color &#10; -Profile (Cladding or Decking) &#10; -If Pick up / Shipment / Delivery &#10; -If with installation" maxlength="500"></textarea>
      Remaining characters: <span id="count"></span>
    </div>
    <div class="form-group">
      <label>
        <input type="checkbox" required data-error="checkbox is required.">
        <em>I agree with the <a data-dismiss="modal" aria-label="Close" data-toggle="modal" data-target=".bs-example-modal-md-5">(privacy policy)</a></em></label>
    </div>
    <button class="tg-theme-btn tg-theme-btn-lg" type="submit">Submit</button>
  </fieldset>
</form>

这是我的完整 form.php 代码:

<?php

//Variable Declaraton

$name = $_POST['name'];
$email = $_POST['email'];
$to_email = "someone@example.com";
$subject = "New Quote";
$phoneNumber = $_POST['phone_number'];
$call = $_POST['call'];
$message = "From: $email\n" . "Client Sender: $name \n" . "Client Number: $phoneNumber\n" . "Call Back?: $call\n" . "\n\n" . "Message: \n\n" .  $_POST['message'];
$headers = 'WPC Cladding & Decking Website <no-reply@aggtrading.com>';


//EMAIL CONFIRMATION
// --- Subject of confirmation email. ---------

$conf_subject = 'Your recent enquiry';

// Who should the confirmation email be from?
$conf_sender = 'AGGTE WPC Cladding & Decking <no-reply@aggtecomposites.com>';
$msg = "Your email: $email\n" . "Your Number: $phoneNumber\n" . "\n" ."Hi" ." ". $_POST['name'] . ",\n\nThank you for your recent enquiry. A member of our team will respond to your message as soon as possible.";
mail( $_POST['email'], $conf_subject, $msg, 'From: ' . $conf_sender );

//----------Send to company email. ---------

/* $send = @mail($to_email,$subject,$message,$headers); */
if(@mail($to_email,$subject,$message,$headers)) {
    echo "<script> location.href='../index.html';
        </script>";
}
else{
    echo "<h1>Message not sent.</h1>";
}

?>

这是发送电子邮件后从 form.php 重定向后应该打开的模式对话框:

<!--my modal-->
<div id="myModal" class="modal fade bs-example-modal-md-2" tabindex="-1" role="dialog">
  <div class="modal-dialog modal-md-2" role="document">
    <div class="modal-content">
      <div class="top_links"><a href="#" data-dismiss="modal" aria-label="Close">Close (X)</a></div>
      <h2 class="modal-title">GET A FREE QUOTE</h2>
      <p>Thank you for sending us message! Feel free to explore more on our website!</p>
    </div>
  </div>
</div>
<!--my modal-->

注意:这是在确认电子邮件发送成功后将重定向到 index.html 的 if/else:

if(@mail($to_email,$subject,$message,$headers)) {
    echo "<script> location.href='../index.html'; </script>";
}
else{
    echo "<h1>Message not sent.</h1>";
}

谢谢!

【问题讨论】:

    标签: php html forms modal-dialog bootstrap-modal


    【解决方案1】:

    先设置一个message变量而不是echo

    类似

    $message = "IT WAS A SUCCESS";
    

    然后使用引导 jQuery 初始化初始化 Modal,并将 Modal 和初始化本身放在 if 语句下。

    if (isset($message)) {
        Modal and initialization here
    }
    

    在 JavaScript 重定向之前设置消息。

    【讨论】:

      猜你喜欢
      • 2015-10-01
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-09
      • 2011-03-04
      相关资源
      最近更新 更多