0

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

这是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);
4

3 回答 3

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();
 }
于 2013-10-25T19:28:43.973 回答
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 文件中添加任何您想做的事情。

于 2014-01-03T16:09:37.017 回答
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;
}

onconfirm 弹出执行代码

$("#yourformId").submit();
于 2013-10-25T19:13:50.043 回答