2013-07-24 52 views
0

我有一個PHP從包括外部process.php,它處理我所有的驗證和發送mail功能,並通過設置
action="<?php echo $_SERVER['PHP_SELF'] ?>"
一切的偉大工程提交給它的自我,它驗證正確和郵件的表單到我的電子郵件,但是當我嘗試在提交後包含header("location: thanks.html")重定向時,它不會重定向。我是否做得不正確或者是否自行提交表單時出現問題?我也在使用Jquery和Jquery Mobile。任何幫助表示讚賞自submiting形式重定向

繼承人我的PHP

<?php 
if(($_SERVER['REQUEST_METHOD'] =='POST') && (!empty ($_POST['action']))): 

if (isset ($_POST['myname'])){$myname=$_POST['myname'];} 
if (isset ($_POST['myphone'])){$myphone=$_POST['myphone'];} 
if (isset ($_POST['myemail'])){$myemail=$_POST['myemail'];} 
if (isset ($_POST['job'])){$job=$_POST['job'];} 
if (isset ($_POST['comments'])){ 
    $comments= filter_var($_POST['comments'], FILTER_SANITIZE_STRING);} 

$formerrors = false; 



     if($myname === '') : 
     $err_myname = '<div class="error"> Sorry, your name is rquired</div>'; 
     $formerrors=true; 
     endif;  

     if($myphone === ''): 
     $err_myphone = '<div class="error"> Sorry, your phone number is rquired</div>'; 
     $formerrors=true; 
     endif;  

     if($myemail === ''): 
     $err_myemaile = '<div class="error"> Sorry, your email is rquired</div>'; 
     $formerrors=true; 
     endif;  

     if($job === ''): 
     $err_job = '<div class="error"> Sorry, your business is rquired</div>'; 
     $formerrors=true; 
     endif;  



if (!($formerrors)) : 
$to = "[email protected]"; 
$subject = " Request from $myname --Show and Tell"; 

$message = "A new show and tell request from:\n 
      $myname \n 
      $myemail\n 
      $myphone\n 
      $job\n 
      $comments\n"; 
$replyto = "From: $myemail"; 


if(mail($to, $subject, $message)): 
    $msg ="Thanks for filling out our form"; 
    header('location: http://localhost/thanks.html'); 
    else: 
    $msg = "There was a problem sending the message"; 
    endif; //mail data 

endif; //check errors 

endif; //form submitted 

?> 

而且我的HTML

<div data-role="content"> 
<?php if (isset($msg)) { echo '<div id="formmessage"><p>', $msg, '</p> </div>';}?> 
<form id="form" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post"> 
    <li data-role="fieldcontain"> 
    <label for="myname">Your Name*:</label><br> 
    <input type="text" name="myname" id="myname" placeholder="John Smith" value="<?php if (isset($myname)) {echo $myname;} ?>" required/> 
    <?php if (isset($err_myname)) {echo $err_myname;} ?> 
    </li> 

    <li data-role="fieldcontain"> 
    <label for="myemail">Email*:</label><br> 
    <input type="email" name="myemail" id="myemail" placeholder="[email protected]" value="<?php if (isset($myemail)) {echo $myemail;} ?>" /> 
    <?php if (isset($err_myemail)) {echo $err_myemail;} ?> 
    <?php if (isset($err_wrong)) {echo $err_wrong;} ?> 
    </li> 

    <li data-role="fieldcontain"> 
    <label for="myphone">Phone number*:</label><br> 
    <input onblur="formatPhone(this);" type="tel" name="myphone" id="myphone" placeholder="1234567890" value="<?php if (isset($myphone)) {echo $myphone;} ?>" required/> 
    <?php if (isset($err_myphone)) {echo $err_myphone;} ?> 
    </li> 

     <li data-role="fieldcontain"> 
    <label for="job">Name of Business*:</label><br> 
    <input type="text" name="job" id="job" placeholder="Where do you work?" value="<?php if (isset($job)) {echo $job;} ?>" required/> 
    <?php if (isset($err_job)) {echo $err_job;} ?> 
    </li> 


    <li data-role="fieldcontain"> 
     <label for="comments">Comments:</label><br> 
     <textarea cols="40" rows="8" name="comments" id="comments" placeholder="Questions or Comments?"> 
     <?php if (isset($comments)) {echo $comments;}?> 
     </textarea> 

    </li> 

<div data-inline="true" data-type="horizontal"> 
<input name="Reset" type="reset" value="Reset" data-role="button" data-inline="true" /> 

<input type="submit" value="Submit" data-role="button" 
data-inline="true" name="action" /> 


</div> 


</form> 



</div> 
+0

將$ msg設置爲$ _SESSION變量,然後在接收頁面中選擇它。只要確保你在腳本的開頭調用了session_start。 – Orangepill

+0

**簡單**:您有'$ msg =「感謝您填寫表單」;'** AND **'header('location:http://localhost/thanks.html');'。你不能擁有兩個。它是一個或另一個。您在標題之前輸出,這就是原因。刪除一個,它將在理論上工作。要重定向,請刪除'$ msg =「的行,感謝您填寫我們的表單」;'。讓我知道它是如何適合你的,如果是的話,那麼我會把它作爲答案。 –

+0

我試着刪除'$ msg',只是有了標題位置,但它仍然不起作用。它是處理表格並郵寄給它,但沒有重定向。 – mhartington

回答

2

1 /簡單的測試,你確定你沒有之前發送一個空白或任何其他字符header()

2 /你有沒有檢查你得到了http://www.php.net/manual/en/outcontrol.configuration.php#ini.output-buffering選項? (否則你必須明確地開始輸出緩衝)

+0

這就是OP的問題,我建議接受你的答案是正確的。 –

+0

@Fred哦,謝謝,我沒有看到那裏的整個討論。 –

+0

不客氣邁克爾。 –