提交後無法重定向表單。表單簡單地從頁面中消失,頁面保持不變。表單提交後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="" > 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>
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;
}
>
即使是不相關的,我建議你更換的重複一堆問題的if/else一個,如果在一個循環/其他集團和某些陣列中的錯誤消息等);) – Biduleohm
@Biduleohm也許這只是缺乏經驗,尤其是與PHP,但在我看來,比我在那裏更累贅和更難:) – user3524234
是的,它只是適當和更短,但功能上是相同的; ) – Biduleohm