duanhe6464 2016-11-22 04:57
浏览 52

提交表格并防止刷新

i'm working on a email sending function on a project. here when i fill the form and after sending it the web site page getting refresh and showing white background page. i need to prevent that from the refreshing and submit the form. here i'l attach the codes and can someone tell me the answer for this question.

HTML code for form

<form class="form-vertical" onsubmit="return sendEmail();" id="tell_a_friend_form" method="post" action="index.php?route=product/product/tellaFriendEmail" enctype="multipart/form-data">
    <div class="form-group ">
     <label class="control-label  ">Your Name <span >*  </span> </label><br>
       <div class="form-group-default">
         <input type="text" id="senders_name" name="sender_name" value="" class="form-control input-lg required" >
       </div>
    </div>

    <div id="notify2" class="">
      <div id="notification-text2" class="xs-m-t-10 fs-12"></div>
      <!--<button type="button" class ="close" id="noti-hide">×</button>-->
      </div>


      <div class="form-group ">
       <label class="control-label  ">Your Email <span >*  </span> </label><br>
         <div class="form-group-default">
           <input type="text" id="sender_email_ID" name="sender_email" value="" class="form-control input-lg" >
         </div>
       </div>

       <div id="notify1" class="">
         <div id="notification-text1" class="xs-m-t-10 fs-12"></div>
           <!--<button type="button" class ="close" id="noti-hide">×</button>-->
           </div>

           <div class="form-group ">
             <label class="control-label">Your Friends' Email <span >* </span></label>
               <p class="lineStyle">Enter one or more email addresses, separated by a comma.</p>

          <div class="form-group-default">
             <input type="text" value="" id="receiver_email" class="form-control required" name="receivers_email" >
             </div>
           </div>

           <div id="notify" class="">
             <div id="notification-text" class="xs-m-t-10 fs-12"></div>
               <!--<button type="button" class ="close" id="noti-hide">×</button>-->
              </div>

            <div >
              <label domainsclass="control-label ">Add a personal message below (Optional) <br></label>
                 <div class="form-group-default">
                   <textarea type="text" id="tell_a_friend_message" name="tell_a_friend_message" class="form-control" rows="10" col="100" style=" width: 330px; height: 100px;"></textarea>
                  </div>
               </div>

               <div id="notify3" class="">
                 <div id="notification-text3" class="xs-m-t-10 fs-12"></div>
                  <!--<button type="button" class ="close" id="noti-hide">×</button>-->
               </div>

               <input type="hidden" name="product_url" id="product_url_field" value="">

               <div class="p-t-15 p-b-20 pull-right">
                 <button id="send_mail_button" class="btn btn-rounded btn-rounded-fl-gold text-uppercase" name="submit" onclick="return sendEmail();" >Send</button>
                  <button id="cancel_email_form" class="btn btn-rounded btn-rounded-gold text-uppercase btn-margin-left" data-dismiss="modal" aria-hidden="true" >Cancel</button>

                </div>

javascript code:

<script>

    function sendEmail() {

        document.getElementById('product_url_field').value = window.location.href
        var emailpattern = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
        var receivers_email = $("#receiver_email").val();
        var sender_email    = $("#sender_email_ID").val();
        var sender_name     = $("#senders_name").val();
        var email_pathname    = window.location.pathname;
        var product_url       = window.location.href;


        if (receivers_email == '') {

                $('#notify').removeClass().addClass("alert-danger");
                $('#notification-text').empty().html("Invalid e-mail or fill the email address correctly");
                $('#notification-text').show();
                setTimeout(function() {
                    $('#notification-text').fadeOut('slow');
                }, 10000);
                return false;

             }
             else {
                 !emailpattern.test(receivers_email);
                  }

        if(sender_name == ''){

            $('#notify2').removeClass().addClass("alert-danger");
            $('#notification-text2').empty().html("please fill the name");
            $('#notification-text2').show();
            setTimeout(function() {
                $('#notification-text2').fadeOut('slow');
            }, 10000);
            return false;
        }


        if (sender_email == '') {

                $('#notify1').removeClass().addClass("alert-danger");
                $('#notification-text1').empty().html("Invalid e-mail or fill the email address correctly");
                $('#notification-text1').show();
                setTimeout(function() {
                    $('#notification-text1').fadeOut('slow');
                }, 10000);
                return false;

         }
        else {
            !emailpattern.test(sender_email);

        }
        $('#notify3').removeClass().addClass("alert-success");
        $('#sender_email').val('');
        $('#notification-text3').empty().html("Email has sent successfully");
        $('#notification-text3').show();
        setTimeout(function() {
            $('#notification-text3').fadeOut('slow');
        }, 10000);
        return true;
    }




</script>

Controller php class:

 public function tellaFriendEmail(){


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

            $receiver_email = $_POST['receivers_email'];
            $name = $_POST['sender_name'];
            $email = $_POST['sender_email'];
            $message = $_POST['tell_a_friend_message'];
            $products_url = $_POST['product_url'];



            $mail = new Mail();
            $mail->protocol = $this->config->get('config_mail_protocol');
            $mail->parameter = $this->config->get('config_mail_parameter');
            $mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
            $mail->smtp_username = $this->config->get('config_mail_smtp_username');
            $mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
            $mail->smtp_port = $this->config->get('config_mail_smtp_port');
            $mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
            $mail->setTo($receiver_email);

            $mail->setFrom($this->config->get('config_email'));
            $mail->setSender("Waltersbay");
            $mail->setSubject($name.' '.'wants you to checkout this product from waltersbay.com');

                    if ($message !=''){
                        $mail->setHtml('Hi Dear,<br/> please checkout the following product that'.' '.$name.' '.'wanted you to see.'.' '.'we hope that you will like it !!!!<br/>'.$products_url.'<br/>'.'<br/> Here is a little message from your friend:<br/>'.$message.'<br/>'.'<br/> Thank you, <br/>  ');
                    }
                      else{
                        $mail->setHtml('Hi Dear,<br/> please checkout the following product that'.' '.$name.' '.'wanted you to see.'.' '.'we hope that you will like it !!!!<br/>'.$products_url.'<br/>'/*.'<br/> Here is a little message from your friend:<br/>'.$message.'<br/>'*/.'<br/> Thank you, <br/>  ');
                      }
            $mail->send();





        }
        else{
            header('location : tella_friend.tpl');


        }
    }
}
  • 写回答

2条回答 默认 最新

  • dongzhi8984 2016-11-22 05:13
    关注

    Put a hidden input in your form. before submitting in your js, fill it with a new key according to time.

    in your php file check if key is duplicate or not? or even if its filled?

    Because js fill this input after clicking the submit button, every time you submit your form you have a new key! If you refresh the form, you're gonna send the previous value again.

    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答