因此,我嘗試爲我的網上商店製作一個PHP表單,客戶可以在其中輸入他們的姓名,地址,電子郵件地址......但驗證似乎無法正常工作。它始終停留在同一頁面(而不是轉到insert.php文件),就好像輸入的數據是錯誤的,即使它不是。對於不在線的店鋪進行PHP表單驗證
<?php
$nameErr = $emailErr = $codeErr = $cityErr = $streetErr = $fakturaErr = "";
$name_ok = $email_ok = $code_ok = $city_ok = $street_ok = 0;
$name = $email = $code = $city = $street = $info = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Pole wymagane";
}
else{
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "Dozwolone tylko litery";
}
else{
$name_ok=1;
}
}
if (empty($_POST["code"])) {
$codeErr = "Pole wymagane";
}
else {
$code = test_input($_POST["code"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[0-9 ]*$/",$code)) {
$codeErr = "Niewłaściwy format kodu";
}
else{
$code_ok=1;
}
}
if (empty($_POST["city"])) {
$cityErr = "Pole wymagane";
}
else{
$city = test_input($_POST["city"]);
// check if city only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$city)) {
$cityErr = "Dozwolone tylko litery";
}
else{
$city_ok=1;
}
}
if (empty($_POST["street"])) {
$streetErr = "Pole wymagane";
}
else {
$street = test_input($_POST["street"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$street)) {
$streetErr = "Dozwolone tylko litery";
}
else{
$street_ok=1;
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
}
else {
$email = test_input($_POST["email"]);
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Niewłaściwy format adresu e-mail";
}
else{
$email_ok=1;
}
}
$action = "";
if($name_ok==1 && $code_ok==1 && $city_ok==1 && $street_ok==1 && $email_ok==1) {
$action = "insert.php";
}
else {
$action = "#";
}
/*if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);
}
*/
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div class="all">
<form action="<?php echo $action; ?>" method="post">
<font color="#A20B0B">*Pola wymagane</font><br><br>
Imię i nazwisko: <input type="text" name="name">
<span class="error"><font color="#A20B0B">* <?php echo $nameErr;?></font></span>
<br><br>
Kod pocztowy:
<input type="text" name="code">
<span class="error"><font color="#A20B0B">* <?php echo $codeErr;?></font></span>
<br><br>
Miasto:
<input type="text" name="city">
<span class="error"><font color="#A20B0B">* <?php echo $cityErr;?></font></span>
<br><br>
Ulica:
<input type="text" name="street">
<span class="error"><font color="#A20B0B">* <?php echo $streetErr;?></font></span>
<br><br>
E-mail:
<input type="text" name="email">
<span class="error"><font color="#A20B0B">* <?php echo $emailErr;?></font></span>
<br><br>
<label>Dodatkowe informacje: <textarea name="info" rows="5" cols="40"></textarea>
<br><br>
Faktura:
<input type="radio" name="faktura" value="tak">Tak
<input type="radio" name="faktura" value="nie">Nie
<span class="error"><font color="#A20B0B">* <?php echo $fakturaErr;?></font></span>
<br><br>
<input type="submit" name="submit" value="Submit" id="register" disabled>
</form>
</div>
你有這個問題,你的Err變量的內容是什麼? – 2014-09-05 12:09:58
如果連續兩次正確輸入所有內容會發生什麼?你可以試試嗎? – 2014-09-05 12:12:29