使用模态弹出联系表单,是否可以在提交时在同一弹出窗口中显示确认?

时间:2013-10-25 18:55:16

标签: javascript php jquery html

我有一个非常基本的php电子邮件联系页面,当用户从模态弹出窗口提交时,该页面被激活。而不是重定向到“thank you.html”页面,是否可以使用“谢谢”消息填充弹出窗口?

这是html:

<!DOCTYPE html>

<head>
<title></title>
<!--Subscribe Popup Function, JQuery -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script>
<script type="text/javascript" src="jquery.reveal.js"></script>
<style>
.reveal-modal-bg {position: fixed; height: 100%; width: 100%; background: #000;    background: rgba(0,0,0,.8); z-index: 100; display: none; top: 0; left: 0; padding: 0px; border: 0px; margin: 0px;}
.reveal-modal {visibility: hidden;top: 100px; width: 80%;background: #fff;position: absolute;z-index: 101;padding: 0px;border: 0px;margin: 0px 10%;}
</style>
</head>

<body>
<a href="#" data-reveal-id="myModal" data-animation="fade">Subscribe</a></div>
<!-- Subscribe Pop-Up Content -->
<div id="myModal" class="reveal-modal">
<form action="receiving.php" method="POST">
<input type="text" name="email" value="EMAIL" /><br>
<input type="submit" name="submit" value="Submit" />
</form>
</div>          
</body>

</html>

这是“receiving.php”:

    <?php 
$email = $_POST['email']; 
if(isset($_POST['submit']))
{
$from_add = "$email"; 
$to_add = "myname@mysite.com"; 
$subject = "New Subscriber";
$message = "Email: $email";
$headers = "From: $from_add \r\n";
$headers .= "Reply-To: $from_add \r\n";
$headers .= "Return-Path: $from_add\r\n";
$headers .= "X-Mailer: PHP \r\n";

if(mail($to_add,$subject,$message,$headers)) 
{
    $msg = "Mail sent";
} 
}
header("location: http://www.mysite.com/thankyou.html"); exit;   
?>

如果有帮助看到这里使用的javascript是“jquery.reveal.js”请注意我对js和php非常缺乏经验。这段代码是我的起点,它来自Reveal JQuery Modal

    (function($) {

$('a[data-reveal-id]').live('click', function(e) {
e.preventDefault();
var modalLocation = $(this).attr('data-reveal-id');
$('#'+modalLocation).reveal($(this).data());
});

$.fn.reveal = function(options) {
var defaults = {  
animation: 'fadeAndPop', //fade, fadeAndPop, none
animationspeed: 300, //how fast animtions are
closeonbackgroundclick: true, //if you click background will modal close?
dismissmodalclass: 'close-reveal-modal' //the class of a button or element that will close an open modal
}; 

//Extend dem' options
var options = $.extend({}, defaults, options); 
return this.each(function() {
var modal = $(this),
topMeasure  = parseInt(modal.css('top')),
topOffset = modal.height() + topMeasure,
locked = false,
modalBG = $('.reveal-modal-bg');

if(modalBG.length == 0) {
modalBG = $('<div class="reveal-modal-bg" />').insertAfter(modal);
}           
//Entrance Animations
modal.bind('reveal:open', function () {
modalBG.unbind('click.modalEvent');
$('.' + options.dismissmodalclass).unbind('click.modalEvent');
if(!locked) {
lockModal();
if(options.animation == "fadeAndPop") {
modal.css({'top': $(document).scrollTop()-topOffset, 'opacity' : 0, 'visibility' : 'visible'});
modalBG.fadeIn(options.animationspeed/2);
modal.delay(options.animationspeed/2).animate({
"top": $(document).scrollTop()+topMeasure + 'px',
"opacity" : 1
}, options.animationspeed,unlockModal());                   
}
if(options.animation == "fade") {
modal.css({'opacity' : 0, 'visibility' : 'visible', 'top': $(document).scrollTop()+topMeasure});
modalBG.fadeIn(options.animationspeed/2);
modal.delay(options.animationspeed/2).animate({
"opacity" : 1
}, options.animationspeed,unlockModal());                   
} 
if(options.animation == "none") {
modal.css({'visibility' : 'visible', 'top':$(document).scrollTop()+topMeasure});
modalBG.css({"display":"block"});   
unlockModal()               
}
}
modal.unbind('reveal:open');
});     

//Closing Animation
modal.bind('reveal:close', function () {
if(!locked) {
lockModal();
if(options.animation == "fadeAndPop") {
modalBG.delay(options.animationspeed).fadeOut(options.animationspeed);
modal.animate({
"top":  $(document).scrollTop()-topOffset + 'px',
"opacity" : 0
}, options.animationspeed/2, function() {
modal.css({'top':topMeasure, 'opacity' : 1, 'visibility' : 'hidden'});
unlockModal();
});                 
}   
if(options.animation == "fade") {
modalBG.delay(options.animationspeed).fadeOut(options.animationspeed);
modal.animate({
"opacity" : 0
}, options.animationspeed, function() {
modal.css({'opacity' : 1, 'visibility' : 'hidden', 'top' : topMeasure});
unlockModal();
});                 
}   
if(options.animation == "none") {
modal.css({'visibility' : 'hidden', 'top' : topMeasure});
modalBG.css({'display' : 'none'});  
}       
}
modal.unbind('reveal:close');
});         

//Open Modal Immediately
modal.trigger('reveal:open')

//Close Modal Listeners
var closeButton = $('.' + options.dismissmodalclass).bind('click.modalEvent', function () {
modal.trigger('reveal:close')    
});

if(options.closeonbackgroundclick) {
modalBG.css({"cursor":"pointer"})
modalBG.bind('click.modalEvent', function () {
modal.trigger('reveal:close')
});
}
$('body').keyup(function(e) {
if(e.which===27){ modal.trigger('reveal:close'); } // 27 is the keycode for the Escape key
});

function unlockModal() { 
locked = false;
}
function lockModal() {
locked = true;
}   

});//each call
}//orbit plugin call
})(jQuery);

3 个答案:

答案 0 :(得分:2)

你可以这样做。邮寄后,用户将用户重定向到当前页面(弹出窗口的位置)

header("location: http://www.mysite.com/#mailsuccess"); //with hash at the last

现在在Jquery中获取此哈希匹配并触发弹出

Jquery的

    $(document).ready(function(){
     var hash=window.location.hash;

      if(hash==="#mailsuccess")
          {
     $('#successModal').reveal({ /* options */ }); //You can use same modal or different modal to show success message
          }    

      });

使用简单的AJAX

<!DOCTYPE html>

<head>
<title></title>
<!--Subscribe Popup Function, JQuery -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.min.js"></script>
<script type="text/javascript" src="jquery.reveal.js"></script>
<style>
.reveal-modal-bg {position: fixed; height: 100%; width: 100%; background: #000;    background: rgba(0,0,0,.8); z-index: 100; display: none; top: 0; left: 0; padding: 0px; border: 0px; margin: 0px;}
.reveal-modal {visibility: hidden;top: 100px; width: 80%;background: #fff;position: absolute;z-index: 101;padding: 0px;border: 0px;margin: 0px 10%;}
</style>

<script type="text/javascript">
$(document).ready(function(){
$(document).on('submit','#mailform',function(e){
       e.preventDefault(); //stop default form submit
       var email=$('#mailform input[name="email"]').val(); //get email from form

       $.ajax({
            type: "POST",
            url: "receiving.php",
            data: "email="+email, 
            cache: false,
            success: function(html){
            if($.trim(html)==='success')
                 { 
                   $('#myModal').trigger('reveal:close'); //close current modal
                   $('#successModal').reveal();  //open success modal
                  }
             }   
            });
    });
});
</script>
</head>

<body>
    <a href="#" data-reveal-id="myModal" data-animation="fade">Subscribe</a></div>
    <!-- Subscribe Pop-Up Content -->
    <div id="myModal" class="reveal-modal">
    <form action="receiving.php" method="POST" id="mailform"> //give a id to form
    <input type="text" name="email" value="EMAIL" /><br>
    <input type="submit" name="submit" value="Submit" />
    </form>
    </div> 

    <div id="successModal" class="reveal-modal">
        Mailed successfully    
    </div>

    </body>

</html>

在PHP中删除标题位置

if(mail($to_add,$subject,$message,$headers)) 
 {
  echo 'success';
  exit();
 }

答案 1 :(得分:1)

以下是您可以编辑和使用下载源代码的资源,或在此处查看实时演示http://purpledesign.in/blog/pop-out-a-form-using-jquery-and-javascript/

像这样添加一个按钮或链接到您的页面

<p><a href="#inline">click to open</a></p>

“#inline”这里应该是包含表格的“id”。

<div id="inline">
 <h2>Send us a Message</h2>
 <form id="contact" name="contact" action="#" method="post">
 <label for="email">Your E-mail</label>
 <input type="email" id="email" name="email" class="txt">
 <br>
 <label for="msg">Enter a Message</label>
 <textarea id="msg" name="msg" class="txtarea"></textarea>
<button id="send">Send E-mail</button>
 </form>
</div>

包含这些脚本以收听点击事件。如果您在表单中定义了操作,则可以使用“preventDefault()”方法

<script type="text/javascript">
 $(document).ready(function() {
$(".modalbox").fancybox();
$("#contact").submit(function() { return false; });
$("#send").on("click", function(){
var emailval = $("#email").val();
var msgval = $("#msg").val();
var msglen = msgval.length;
var mailvalid = validateEmail(emailval);
if(mailvalid == false) {
$("#email").addClass("error");
}
else if(mailvalid == true){
 $("#email").removeClass("error");
 }

 if(msglen < 4) {
 $("#msg").addClass("error");
 }
 else if(msglen >= 4){
 $("#msg").removeClass("error");
 }

 if(mailvalid == true && msglen >= 4) {
 // if both validate we attempt to send the e-mail
 // first we hide the submit btn so the user doesnt click twice
 $("#send").replaceWith("<em>sending...</em>");
 //This will post it to the php page
 $.ajax({
 type: 'POST',
 url: 'sendmessage.php',
 data: $("#contact").serialize(),
 success: function(data) {
 if(data == "true") {
 $("#contact").fadeOut("fast", function(){
//Display a message on successful posting for 1 sec
 $(this).before("<p><strong>Success! Your feedback has been sent, thanks :)</strong></p>");
 setTimeout("$.fancybox.close()", 1000);
 });
 }
 }
 });
 }
 });
 });
</script>

您可以在PHP文件中添加任何想要做的事情。

答案 2 :(得分:0)

确定这只是为您的表单添加ID 像:

<form action="receiving.php" method="POST" id="yourformId">

用户javascript函数 不要使用提交按钮用户正常按钮

<input type="button" name="submit" value="Submit" onclick="beforesubmit()" />
function beforesubmit()
{
    /**open popup**/ 
    return false;
}

确认弹出执行代码

$("#yourformId").submit();