2015-02-08 122 views
1

如何在用户使用“注册”按钮提交表单并在表单将电子邮件发送给antoniya后,将此表单重定向到外部网站,例如http://amaze.bg。用户输入的p?amaze.bg的详细信息?表单提交后重定向

这里是PHP代码(contact_process.php):

<?php 

usleep(500000); 
$to = "[email protected]"; 
$author = ""; 
$email = ""; 
$comment = ""; 
$class = "FORM CONTACT "; 

if (isset($_POST['input_name'])) 
    $author = $_POST['input_name']; 
if (isset($_POST['input_email'])) 
    $email = $_POST['input_email']; 
if (isset($_POST['input_class'])) 
    $class = $_POST['input_class']; 
if (isset($_POST['input_message'])) 
    $comment = $_POST['input_message']; 

$sub="Alumni registration recieved - ".$date1; 
     $name=$authorlast."< ".$email." >"; 

    $msg = ''; 
    if (@mail($to,$sub,$body,"Content-Type: text/html; charset=iso-8859-1")) 
     { 
      $msg = 'Your registration was sent successfully'; 
      //echo '<div class="alert success pngfix">'. $msg .'</div>'; 
     } 
     else{ 
      $msg = 'Email failed to be sent (mail function not work)'; 
      //echo '<div class="alert error pngfix">'. $msg .'</div>'; 
     } 

    echo $msg; 


?> 

这里是.js文件部分:

jQuery.validator.addMethod(
     "math", 
     function(value, element, params) { 
      if (value==='') 
       return false; 
      return this.optional(element) || value == params[0] + params[1]; 
     }, 
     jQuery.format("Please enter the correct value for {0} + {1}") 
    ); 
    $('.form-register').validate({ 
     rules: { 
      input_name: { 
       minlength: 3, 
       required: true 
      }, 
      input_email: { 
       required: true, 
       email: true 
      }, 
      input_message: { 
       minlength: 0, 
       required: false 
      } 
      , 
      submitHandler: function(form) { 
      var a=$('.form-register').serialize(); 
      $.ajax({ 
       type: "POST", 
       url: "contact_process.php", 
       data:a, 
       complete:function(){ 
       }, 
       beforeSend: function() { 

       }, 
       success: function(data){ 
        if (data=='success') { 
         $('.form-register').find("input[type=text], textarea").val(""); 
         alert('You have successfully registered. Thank you for being active alumni!'); 
        } else { 
         alert(data); 
        } 
       }, 
       error : function() { 

       } 
      }); 
      return false; 
     } 
    }); 


}); 

谢谢你的帮助。

回答

0

在你的成功回调,添加位置=“http://amaze.bg

success: function(data) { 
    // if you echo "success" in your php script when the mail is sent 
    if (data === 'success') { 
     $('.form-register').find("input[type=text], textarea").val(""); 
     alert('You have successfully registered. Thank you for being active alumni!'); 
     // Now redirect 
     location = "http://amaze.bg" 
    } else { 
     // if you echo "Some Error Message" from your php script 
     alert(data); 
    } 
}, 

最终,你应该从PHP发送正确的头,这样的成功钩在$就火在200个状态码在遇到400-500状态码时,$ .ajax会触发错误钩子。请看一下HTTP Status CodesSending Proper Headers in PHP

+0

我添加到成功回调的位置,但没有改变。用户提交表单后,会将信息发送到服务器,并显示警报消息“您的注册已成功发送”,但不会将用户重定向到外部URL。在使用按钮提交所有输入的信息后,我应该如何更改代码,以便表单将用户重定向到http://example.com? – 2015-02-14 22:38:54

0

经过查理的一些实验和帮助,我设法找到了代码中的哪些地方和什么东西,以便表单将信息发送到服务器,并将用户重定向到另一个网站。应在.js文件进行的改变与添加以下行的文件:

location = "http://amaze.bg/" 

但应在“其他”中的.js文件被加入其中,“警报”是“成功”下为一切正常工作:

success: function(data){ 
      if (data=='success') { 

      alert(data); 

      } else { 

      location = "http://amaze.bg/" 

      } 
     }, 
      error : function() { 

      }