2015-02-08 173 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() { 

      }