2014-04-11 54 views
1

提交後無法重定向表單。表單簡單地從頁面中消失,頁面保持不變。表單提交後PHP消失,而不是通過標題位置重定向

歡迎任何意見。使用Twitter Bootstrap,GoDaddy託管(我知道)。

FORM:

<div class="row"> 
    <div class="col-lg-7 col-lg-offset-3"> 
     <form class="well" role="form" id="colform" method="post" action="<?php $_SERVER['PHP_SELF'];?>"> 
      <div class="row"> 
       <div class="col-lg-3 pull-left error"> 
        <p class="text-left">* - Required Fields</p> 
       </div> 
      </div> 
      <div class="row"> 
       <div class="form-group col-sm-4"> 
        <label class="control-label" for="name">Your Name *</label> 
        <input type="text" class="form-control" name="name"> 
        <span class="help-block error"><?php echo $nameErr;?></span> 
       </div> 
       <div class="form-group col-sm-4"> 
        <label class="control-label" for="phone_number">Phone Number *</label> 
        <input type="text" class="form-control" name="phone_number"> 
        <span class="help-block error"><?php echo $phone_numberErr;?></span> 
       </div> 
       <div class="form-group col-sm-4"> 
        <label class="control-label" for="email">Email</label> 
        <input type="text" class="form-control" name="email"> 
        <span class="help-block error"><?php echo $emailErr;?></span> 
       </div> 
      </div> 

      <div class="row"> 
       <div class="form-group col-sm-4"> 
        <label class="control-label" for="city">City *</label> 
        <input type="text" class="form-control" name="city"> 
        <span class="help-block error"><?php echo $cityErr;?></span> 
       </div> 
       <div class="form-group col-sm-4"> 
        <label class="control-label" for="post_code">Post Code *</label> 
        <input type="text" class="form-control" name="post_code"> 
        <span class="help-block error"><?php echo $post_codeErr;?></span> 
       </div> 
       <div class="form-group col-sm-4"> 
        <label class="control-label" for="full_address">Full Address *</label> 
        <input type="text" class="form-control" name="full_address"> 
        <span class="help-block error"><?php echo $full_addressErr;?></span> 
       </div> 
      </div> 

      <div class="form-group row"> 
       <div class="input-group col-md-6 col-md-offset-3"> 
        <span class="input-group-addon">Quantity of bags you have: *</span> 
        <input type="text" class="form-control" name="bags"> 
       </div> 
      </div> 

      <div class="row form-group"> 
       <div class="col-sm-10 col-sm-offset-1"> 
        <label class="" >&nbsp; &nbsp;Please leave any additional information you would think is important to mention:</label> 
        <textarea class="form-control" rows="3" name="comments"></textarea> 
       </div> 
      </div> 

      <div class="row form-group"> 
       <div class="col-sm-10 col-sm-offset-1"> 
        <div class="checkbox"> 
         <label> 
          <input type="checkbox" name="updates"> If you would like to receive news and updates on our services, please thick this box 
         </label> 
        </div> 
       </div> 
      </div> 

      <div class="alert alert-danger"><span class="label label-danger">IMPORTANT !</span> &nbsp; &nbsp; 
       Each item in the bag is checked, please make sure that it doesn't contain following items: </br></br> 
       <div class="text-center"><strong>wet, dirty, ripped, bad quality or old fashioned clothing(90s, 80s or older).</br> 
        underwear, school uniform, work clothes.</strong></div></br> 

       <div class="text-center small">It will save your precious time and prevent any inconvenience that might occur during the process, which we always try to make no longer than 10-12mins no matter the quantity.</br> 
        Can't check everything or not sure what's inside the bags?</br> Not a problem at all, just make a note of that in the comment box above. 
       </div> 
      </div> 
      <div class="row"> 
       <div class="col-sm-6 col-sm-offset-3"> 
        <button type="submit" class="btn btn-primary btn-lg btn-block" name="fsubmit" >Send Request</button> 
       </div> 
      </div> 

     </form> 
    </div> 
</div> 

PHP代碼僅僅是高於這個HTML:

<?php 
// define variables and set to empty values 
$nameErr = $emailErr = $cityErr = $post_codeErr = $full_addressErr = $phone_numberErr = ""; 
$name = $email = $city = $comment = $post_code = $full_address = $phone_number = $bags = ""; 
$nameCheck = $cityCheck = $post_codeCheck = $full_addressCheck = $phone_numberCheck = $emailCheck = false; 

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{ 
    /* CHEKING NAME FIELD */ 
    if (empty($_POST["name"])) 
     { $nameErr = "Name is required"; } 
    else { 
      $name = test_input($_POST["name"]); 
      if (!preg_match("/^[a-zA-Z ]*$/",$name)) 
      { $nameErr = "Only letters and white space allowed"; } 
      else 
       { $nameCheck=true; } 
     } 

    /* CHECKING CITY FIELD */ 
    if (empty($_POST["city"])) 
     { $cityErr = "City is required"; } 
    else { 
      $city = test_input($_POST["city"]); 
      if (!preg_match("/^[a-zA-Z ]*$/",$city)) 
      { $cityErr = "Only letters and white space allowed"; } 
      else 
       { $cityCheck=true; } 
     } 

    /* CHECKING POST CODE FIELD*/ 
    if (empty($_POST["post_code"])) 
     { $post_codeErr = "Post Code is required"; } 
    else { 
      $post_code = test_input($_POST["post_code"]); 
      if (!preg_match("/^[a-zA-Z \d]*$/",$post_code)) 
      { $post_codeErr = "Only letters, whole numbers and white space allowed"; } 
      else 
       { $post_codeCheck=true; } 
     } 

    /* CHECKING FULL ADDRESS FIELD */ 
    if (empty($_POST["full_address"])) 
     { $full_addressErr = "Full address is required"; } 
    else { 
      $full_address = test_input($_POST["full_address"]); 
      if (!preg_match("/^[a-zA-Z \d,.]*$/",$full_address)) 
      { $full_addressErr = "Only letters, whole numbers, commas, dot and white space allowed"; } 
      else 
       { $full_addressCheck=true; } 
     } 

    /* CHECKING PHONE FIELD */ 
    if (empty($_POST["phone_number"])) 
     { $phone_numberErr = "Phone number is required"; } 
    else { 
      $phone_number = test_input($_POST["phone_number"]); 
      if (!preg_match("/^[ \d]*$/",$phone_number)) 
      { $phone_numberErr = "Only whole numbers and white space allowed"; } 
      else 
       { $phone_numberCheck=true; } 
     } 

    /* CHECKING BAG FIELD */ 
    if (empty($_POST["bags"])) 
     { $bags = ""; } 
    else 
     { $bags = test_input($_POST["bags"]); } 

    /* CHECKING EMAIL FIELD */ 
    if (empty($_POST["email"])) 
     { $email = "No email entered"; 
      $emailCheck=true; } 
    else { 
      $email = test_input($_POST["email"]); 
      if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) 
       { $emailErr = "Invalid email format"; } 
      else 
       { $emailCheck=true; } 
      } 

    /* CHECKING COMMENTS FIELD */ 
    if (empty($_POST["comment"])) 
     { $comment = ""; } 
    else 
     { $comment = test_input($_POST["comment"]); } 

    if ($nameCheck && $cityCheck && $post_codeCheck && $full_addressCheck && $phone_numberCheck && $emailCheck) 
     { $email = $_POST['email'] ; 
     $subject = date("d.M H:i") ; 
     $message = "Name: " . $_POST['name'] . "\n" . "City: " . $_POST['city'] . "\n" . "Post Code: " . $_POST['post_code'] . "\n" . "Full Address: " . $_POST['full_address'] . "\n" . "Phone Number: " . $_POST["phone_number"] . "\n" . "Email: " . $_POST["email"] . "\n" . "Number of bags: " . $_POST["bags"] . "\n" . "Comments: " . $_POST["comments"] . "\n" . "Would like to receive updates: " . $_POST['updates']; 
     mail("[email protected]", "$subject", $message, "From:" . $email); /* Send Email */ 
     header("Location: http://www.google.com"); 
     exit(); 
     } 
} 

function test_input($data) 
{ 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
}    

>

+1

即使是不相關的,我建議你更換的重複一堆問題的if/else一個,如果在一個循環/其他集團和某些陣列中的錯誤消息等);) – Biduleohm

+0

@Biduleohm也許這只是缺乏經驗,尤其是與PHP,但在我看來,比我在那裏更累贅和更難:) – user3524234

+0

是的,它只是適當和更短,但功能上是相同的; ) – Biduleohm

回答

0

你需要在你的代碼中沒有頂部用頭?頁面上的任何輸出。

例如,如果您使用的是這樣的代碼它無法正常工作:

echo "hi"; 
header("location: http://www.google.com"); 

你不能輸出頭命令之前的任何數據。

你應該試着改變你的代碼位置

+0

我已經重定向到谷歌的網頁..!在這裏發佈之前檢查你的代碼..! – Elavarasan

+0

@phpniki所以我可以把一個函數放在html標記之前,然後在頁面加載和成功驗證表單之後調用該函數? – user3524234

+0

謝謝你,把所有的php代碼都放在標籤上,現在它可以正常工作。 – user3524234