2015-11-05 61 views
-1

我已經創建了一個php註冊頁面。一切運行良好。當我將文本框留空時,它顯示一個錯誤。註冊重定向,即使有驗證錯誤

但是,當我使用動作方法重定向註冊頁面時,它總是重定向,無論是當我正在寫東西的時候,還是當我沒有在文本框中輸入內容時。

我希望頁面在字段爲空時不重定向。我使用布爾值和break方法,但沒有發生任何事情。

任何人都可以幫助我在這個問題上?

<html> 
<?php 
include 'header.php'; 

$nameErr = $emailErr = $genderErr = $passwordErr = $addErr = $phoneErr = ""; 

$fullname = $email = $gender = $password = $address = $phone = ""; 
$flag = false; 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 

    if (empty($_POST["fullname"])) { 
    $nameErr = "Name is required"; 
    break; 
    $flag  
    } 
    else { 
    $fullname = test_input($_POST["fullname"]); 

    } 


    if (empty($_POST["password"])) { 
    $passwordErr = " password required"; 
    break; 
    } 
    else { 
    $password = test_input($_POST["password"]); 
    } 

    if (empty($_POST["email"])) { 
    $emailErr = "Email is required"; 
    break; 
    } 
    else { 
    $email = test_input($_POST["email"]); 
    } 



    if (empty($_POST["address"])) { 
    $addErr = "Address required"; 
    break; 
    } 
    else { 
    $gender = test_input($_POST["address"]); 
    } 

    if (empty($_POST["gender"])) { 
    $genderErr = "Gender is required"; 
    break; 
    } else { 
    $gender = test_input($_POST["gender"]); 
    } 

    if (empty($_POST["phone"])) { 
    $phoneErr = "phone no required"; 
    break; 
    } else { 
    $phone = test_input($_POST["phone"]); 
    } 
} 
?> 
<body> 
<br> 
<div class="regis_div" > 
    <form name="myForm" <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="post" action="thanks.php" > 

    <table class="table2" border="0" align="center" cellspacing="15" > 
    <tr> 
    <td colspan="3"><h1><center>User Registration Form</center></h1></td> 
    </tr> 
    <tr> 
    <td width="291"> &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Full name:</td> 
    <td width="150"><input type="text" name="fullname"></td> 
    <td><span class="error"><font size="2" color="red">* <?php echo $nameErr;?></span></font></td> 
</tr> 
<tr> 
    <td>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Password</td> 
    <td><input type="password" name="password"></td> 
    <td><span class="error"><font size="2" color="red">* <?php echo $passwordErr;?></span></td> 
    </tr> 
<tr> 
    <td>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Confirm Password</td> 
    <td><input type="password" name="Confirmpassword"></td> 
    <td><span class="error"><font size="2" color="red">* <?php echo $passwordErr;?></span></td> 
    </tr> 
    <tr> 
    <td>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Email Address:</td> 
    <td><input type="text" name="email" ></td> 
    <td><span class="error"><font size="2" color="red">* <?php echo $emailErr;?></span></td> 
    </tr> 
    <tr> 
    <td>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Gender:</td> 
    <td><input type="radio" name="gender" value="female">Female 
     <input type="radio" name="gender" value="male">Male</td> 
     <td><span class="error"><font size="2" color="red">* <?php echo $genderErr;?></span></td> 
    </tr> 
    <tr> 
    <td height="80">&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Address:</td> 
    <td><textarea type="text" name="Address" class="address" rows="4" cols="20" ></textarea></td> 

    <td><span class="error"><font size="2" color="red">* <?php echo $addErr;?></span></td> 
    </tr> 

    <tr> 
    <td>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;Phone No:</td> 
    <td><input type="text" name="phone"></td> 
    <td><span class="error"><font size="2" color="red">* <?php echo $phoneErr;?></span></td> 
    </tr> 
    <tr> 
    <td colspan="2"><input class="regbtn2" type="submit" value="Submit" align="center" ></td> 
    </tr> 
    </table> 
    </form> 
    </div> 



</body> 
</html> 
+1

這真的不清楚你想要什麼來描述。 – David

+0

當我沒有寫任何文本框中的任何東西,並提交表單提交成功沒有任何錯誤,並沒有任何錯誤,並重定向到新的頁面,但我想只有當表單是字段提交成功的條件。 –

回答

1
if (empty($_POST["gender"])) { 
    $genderErr = "Gender is required"; 
    break; 
} else { 
    $gender = test_input($_POST["gender"]); 
} 

if (empty($_POST["phone"])) { 
    $phoneErr = "phone no required"; 
    break; 
} else { 
    $phone = test_input($_POST["phone"]); 
} 

break只會爆發forforeachwhiledo-whileswitch結構,不if語句。

它也不清楚究竟腳本,你認爲應該處理您的表單提交:

<form name="myForm" <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> method="post" action="thanks.php" > 

如果您在上面張貼的腳本名爲form.php,那麼上面的線會產生以下(無效)的標記:

<form name="myForm" form.php method="post" action="thanks.php" > 

交房時,執行會立即流向thanks.php,沒有你的PHP錯誤檢查(如上面張貼)將運行。如果你想你上面貼到運行的代碼,你應該是表單標籤改成這樣:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> 

然後,一旦你確信你的提交沒有錯誤,你可以將用戶重定向到新的一頁:

header('Location: thanks.php'); 

你還應該注意的是,這一行被執行的時候,你將失去訪問所有現有的POST數據的,所以如果你想保留它你必須用它做什麼之前重定向一個標題。

但是,您的代碼存在很多問題。

if (empty($_POST["fullname"])) { 
    $nameErr = "Name is required"; 
    break; // Already mentioned this line will have no effect 
    $flag // Why is this line here? 
} 

怎麼你就會知道,用當前的結構,如果遇到任何錯誤?它必須是這樣的:

if(isset($nameErr) || isset($passwordErr) || isset($emailErr) || ...) 

可以簡化與數組:

if (empty($_POST["fullname"])) { 
    $errors['name'] = "Name is required"; 
    // ... 
} 

// ... 

if(count($errors)) { 
    // Some error occurred, don't redirect 
} else { 
    // ... do something with POST data ... 
    header('Location: thanks.php'); 
    exit; // Always exit or die after issuing a header redirect 
} 

和相關的標記:

<?php if(isset($errors['name'])) { ?> 
<span class="error"> 
    <font size="2" color="red">* 
     <?php echo $errors['name']; ?> 
    </font> <!-- Don't forget to close all of your opened HTML tags --> 
</span> 
<?php } ?>