表单提交后的PHP消失,而不是通过标头位置重定向

时间:2014-04-11 14:56:36

标签: php twitter-bootstrap-3 http-headers

提交后无法重定向表单。表单只是从页面上消失,页面保持不变。

欢迎任何输入。使用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("collections@c4c-essex.co.uk", "$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;
}               

&GT;

1 个答案:

答案 0 :(得分:0)

您需要在代码顶部使用标题而页面上没有任何输出。

例如,如果你使用这样的代码,它就无法正常工作:

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

你不能在header命令之前输出任何数据。

您应该尝试更改代码位置