简体   繁体   English

提交表单模式后显示成功消息

[英]Display success message after submit form modal

I have a contact form linked with form-to-email.php to send emails.我有一个与 form-to-email.php 链接的联系表格来发送电子邮件。 What happen is when you click submit it will redirect you to another page (new one) what I would like to have is to show/display the success message within the modal right after clicking the botton.发生的情况是,当您单击提交时,它会将您重定向到另一个页面(新页面),我想要的是在单击按钮后立即在模态中显示/显示成功消息。

<div class="modal fade" id="modal-register" tabindex="-1" role="dialog" aria-labelledby="modal-register-label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
                        <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal">
                            <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
                        </button>
                        <h4 class="modal-title" id="modal-register-label">اشتراك</h4>
                    </div>
<div class="modal-body">
<form method="post" name="myemailform" action="form-to-email.php">
    <div class="row">
        <div class="col-xs-12 input-group">
          <input type="radio" name="form-busnisstype" value="company" id="company">
          <label for="company">شركة</label>
          <input type="radio" name="form-busnisstype" value="freelaancer" id="freelancer">
          <label for="freelancer">مستقل</label>
      </div>
      <div class="col-xs-12 input-group">
        <input type="text" name="form-name" id="name" placeholder="الاسم الكريم" required="required">
      </div>
      <div class="col-xs-12 input-group">
        <input type="email" name="form-email" id="email" placeholder="البريد الالكتروني" required="required">
      </div>
      <div class="col-xs-12 input-group">
          <input type="tel" pattern="^[0-9]*$" name="form-phone" id="phone" placeholder="05xxxxxxxx" required="required">
      </div>
        <div class="col-xs-12 input-group">
          <select name="form-officetype" id="officetype" required="required">
                <option value="" disabled selected hidden="" >اختر مكتبك</option>
                <option value="Gold">individual</option>
                <option value="Silver">room</option>
                <option value="Bronze">meeting</option>
                <option value="Basic">orgonization</option>
            </select>
        </div>
        <div class="col-xs-12 input-group">
          <select name="form-membership" id="membership" required="required">
                <option value="" disabled selected hidden="" >اختر باقتك</option>
                <option value="Gold">Gold</option>
                <option value="Silver">Silver</option>
                <option value="Bronze">Bronze</option>
                <option value="Basic">Basic</option>
            </select>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12 input-group input-group-icon">
        <input placeholder="بداية الاشتراك" type="text" onfocus="(this.type='date')" id="booking" name="form-booking" required="required">
      </div>
        <div class="col-xs-12 input-group input-group-icon">
          <select name="form-person" id="person" required="required">
          <option value="" disabled selected hidden="" >كم عدد الاشخاص</option>
                <option value="1">1 Person</option>
<option value="2">2 People</option><option value="3">3 People</option><option value="4">4 People</option><option value="5">5 People</option>
            </select>
        </div>
        <textarea id="mep_msg" name="form-message" rows="" Placeholder="Comment" required="required"></textarea>
        <input class="send_btn" type="submit" name='submit' value="submit">

      </div>    
    </form>

<script language="JavaScript">
var frmvalidator  = new Validator("myemailform");
frmvalidator.addValidation("name","req","Please provide your name"); 
frmvalidator.addValidation("email","req","Please provide your email"); 
frmvalidator.addValidation("tel","tel","Please enter a valid email address"); 
frmvalidator.addValidation("email","email","Please enter a valid email address"); 
</script>
</div>
</div>
</div>
</div>

<!--Success pop up Starts-->
<div class="modal fade" id="success_msg" role="dialog" tabindex="-1">
<div class="success">
  <div class="modal-dialog-success">
    <div class="col-xs-12 pade_none">
      <button type="button" class="close" onClick="closeConfirm();" data-dismiss="modal">&times;</button>
      <div class="col-xs-12 pade_none">
        <h2>Success!</h2>
        <p>Your message has been sent.</p>
      </div>
      <div class="col-xs-12 pad_none">
      </div>
    </div>
  </div>
</div>
</div>
<!--Success pop up ends-->

and the form-to-email.php is the following:和 form-to-email.php 如下:

<?php

/* Configuration */
$subject = 'Membership Enquiry'; // Set email subject line here
$mailto  = 'email here'; // Email address to send form submission to
/* END Configuration */

$name = $_POST['form-name'];
$visitor_email = $_POST['form-email'];
$message = $_POST['form-message'];
$phone = $_POST['form-phone'];
$busnisstype = $_POST['form-busnisstype'];
$officetype = $_POST['form-officetype'];
$membership = $_POST['form-membership'];
$person = $_POST['form-person'];
$booking = $_POST['form-booking'];

// HTML for email to send submission details
$body = "
<br>
<p>The following information was submitted through the contact form on your website:</p>
<p><b>الاسم</b>: $name<br>
<b>الايميل</b>: $visitor_email<br>
<b>رقم الجوال</b>: $phone<br>
<b>نوع المشأة</b>: $busnisstype<br>
<b>المكتب المرغوب</b>: $officetype<br>
<b>العضوية</b>: $membership<br>
<b>عدد الاشخاص</b>: $person<br>
<b>التاريخ المتوقع لبدء الاشتراك</b>: $booking<br>
<p>ملاحضات اخرى: <b>$message</b></p>
";


$headers = "From: $name <$visitor_email> \r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = "<html><body>$body</body></html>";

if (mail($mailto, $subject, $message, $headers)) {
    header('Location: thank-you.html');
} else {
    echo 'Form submission failed. Please try again...'; // failure
}

?>

You're redirecting to thank-you.html.您正在重定向到thank-you.html。 Add there the code (with the success message) and if you want it to be a modal just add the modal code:在那里添加代码(带有成功消息),如果您希望它成为模态,只需添加模态代码:

<!--Success pop up Starts-->
<div class="modal fade" id="success_msg" role="dialog" tabindex="-1">
<div class="success">
  <div class="modal-dialog-success">
    <div class="col-xs-12 pade_none">
      <button type="button" class="close" onClick="closeConfirm();" data-dismiss="modal">&times;</button>
      <div class="col-xs-12 pade_none">
        <h2>Success!</h2>
        <p>Your message has been sent.</p>
      </div>
      <div class="col-xs-12 pad_none">
      </div>
    </div>
  </div>
</div>
</div>
<!--Success pop up ends-->

And launch a javascript when entering:并在输入时启动 javascript:

<body onload="myOnloadFunc();">

Your function should show modal:您的函数应显示模态:

function myOnloadFunc() {
    $('#success_msg').modal('show');
}

You do two things either use AJAX to send request to form-to-email.php script or create hidden iframe with id="email" name="email" and use target="email" in form tag, it will open your php script in iframe and execute it but it will not redirect to that page, then you can use您做了两件事,要么使用 AJAX 将请求发送到 form-to-email.php 脚本,要么使用id="email" name="email"创建隐藏的 iframe 并在表单标签中使用target="email" ,它将打开您的 php iframe 中的脚本并执行它但它不会重定向到该页面,然后您可以使用

document.getElementById('email').addEventListener('load', function() {
    alert('Email Send Successfully');
});

instead of alert you can add toaster (green bar) to your modal.您可以将烤面包机(绿色条)添加到您的模态中,而不是警报。

PHP CODE PHP代码

    <?php session_start();
    include('config/connect_database.php');

    $Username = '';

    if(isset($_POST['Login'])) {

        $Username = mysqli_real_escape_string($conn, $_POST['Username']);
        $Password = mysqli_real_escape_string($conn, $_POST['Password']);

        $sql = "SELECT Username, Password, Authority FROM user WHERE Username LIKE '$Username' AND Status LIKE 'Active'";

        $result = mysqli_query($conn, $sql);    

        if(mysqli_num_rows($result) > 0) {
            $username = mysqli_fetch_assoc($result);

            mysqli_free_result($result);
            mysqli_close($conn);

            if(($Username === $username['Username']) && (password_verify($Password, $username['Password']))) {

                $_SESSION['Username'] = $_POST['Username'];
                $_SESSION['Authority'] = $username['Authority'];

            } else {
                echo "<script>alert('Invalid Username or Password');</script>";         
            }
        } else {
            echo "<script>alert('Invalid Username or Password');</script>";     
        }   

    }
?>

HTML CODE代码

    <!DOCTYPE html>
<html>
<head>
    <title>Management System</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <script>
    $(document).ready(function(){ 
        <?php if(isset($_SESSION['username'])){?>$('#modalNotification').modal();<?php } ?> 
        $('div.modal button').click(function(){ location.href='home.php'; });
    });
    </script>
</head>

<body>
    <nav>
        <a class="navbar-brand" href="#">
            <img src="Source/pms_logo_blue.png" alt="Logo" style="width:50px;">
        </a>

        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapsibleNavbar">
             <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse" id="collapsibleNavbar">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item">
                    <a class="nav-link" href="register.php">Register</a>
                </li>
            </ul>
        </div>  
    </nav>

    <div class="modal fade" id="modalNotification" tabindex="-1" role="dialog" aria-labelledby="modalNotificationTitle" aria-hidden="true">
      <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="modalNotificationTitle">NOTIFICATION</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <p>CONTENT</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>

    <div class="container">
        <div class="row justify-content-center py-5">
            <div class="col" style="max-width: 400px;">
                <h3 style="text-align: center;">Flinken Production Management System</h3>
            </div>
        </div>

        <div class="row justify-content-center">
            <div class="col" style="max-width:400px;">
                <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
                    <div class="form-group">
                        <label>Enter Email</label>
                        <input type="email" name="Username" value="<?php echo htmlspecialchars($Username); ?>" class="form-control" required>
                    </div>

                    <div class="form-group">
                        <label>Enter Passsword</label>
                        <input type="password" name="Password" class="form-control" required>
                    </div>

                    <div class="form-group">
                        <button type="submit" name="Login" value="True" class="btn btn-primary btn-block">Login</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <footer class="section">
        <div class="text py-5" style="text-align: center">Copyright </div>
    </footer>

</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM