2013-02-16 166 views
1

我想驗證我的表單,因此所有字段都是必需的。如果一個字段沒有插入或留空,它會在提交後顯示錯誤消息。誰能幫忙?使用PHP進行表單驗證

形式

<html> 
<head> 
<title>Form Input Data</title> 
</head> 
<table> 
<body><table border="1"> 
<table bgcolor="lightblue"></body> 

    <form method="post" action="insert_ac.php"> 
    <br> 
<tr><td align="left"><strong>Nurse Information</strong></td></tr> 
<tr> 
<td><font color="red">Please select your name</font></td> 
</tr> 
<tr> 
<td>Fullname</td> 
<td><select name="valuelist">; 
<option value="valuelist" name="nurse_name" value='<?php echo $nurse_name; ?>'></option> 
<?php 
$value=$_POST ["valuelist"]; 
$con = mysql_connect("localhost","root","") or die('Could not connect:'.mysql_error()); 
mysql_select_db("a&e", $con) or die('Could not select database.'); 

$fetch_nurse_name = mysql_query("SELECT DISTINCT Fullname FROM nurse"); 

while($throw_nurse_name = mysql_fetch_array($fetch_nurse_name)) { 
echo '<option value=\"'.$throw_nurse_name[0].'">'.$throw_nurse_name[0].'</option>'; 
} 
echo "</select>"; 

?> 
</td> 
</tr> 
<tr> 
<td>Please register name here:</td> 
<tr> 

     <td>Fullname</td> 

     <td><input type="text" name="nurse_forename" size="30"> </td> 

    </tr> 
</tr> 
+0

請,顯示HTML .. – 2013-02-16 21:43:38

回答

2

我會做這樣的事情:

$req = ['field1', 'field2', 'field...']; 
$status = true; 
foreach ($req as $field) { 
    if (empty($_POST[$field])) { 
     echo 'Field ' . $field . ' is empty'; 
     $status = false; 
    } 
} 
if ($status) { 
    // ok 
} else { 
    // not okay! 
} 

您創建一個數組($req),超過他們所有的字段名和循環。檢查每個字段對empty()(檢查PHP手冊的這個功能)。

這是一個更好的(也是最主要的)正確的HTML代碼片段...請正確縮進並閱讀任何HTML格式的代碼。你的HTML是**。

<?php 

$value=$_POST["valuelist"]; 
$con = mysql_connect("localhost","root","") or die('Could not connect:'.mysql_error()); 
mysql_select_db("a&e", $con) or die('Could not select database.'); 

$fetch_nurse_name = mysql_query("SELECT DISTINCT Fullname FROM nurse"); 

?> 
<html> 
<head> 
    <title>Form Input Data</title> 
</head> 
<body> 

    <form method="post" action="insert_ac.php"> 

    <table border="1" bgcolor="lightblue"> 
     <tr> 
      <td align="left"><strong>Nurse Information</strong></td> 
     </tr> 
     <tr> 
      <td><font color="red">Please select your name</font></td> 
     </tr> 
     <tr> 
      <td>Fullname</td> 
      <td> 
       <select name="valuelist"> 
        <option value="valuelist" value="<?php echo $nurse_name; ?>"></option> 
        <?php 

        while($throw_nurse_name = mysql_fetch_array($fetch_nurse_name)) { 
         echo '<option value="'.$throw_nurse_name[0].'">'.$throw_nurse_name[0].'</option>'; 
        } 
        ?> 
       </select> 
      </td> 
     </tr> 
     <tr> 
      <td>Please register name here:</td> 
     </tr> 
     <tr> 
      <td>Fullname</td> 
      <td><input type="text" name="nurse_forename" size="30"> </td> 
     </tr> 
    </table> 
    </form> 
</body> 
</html> 

如果你只有兩個給定的領域,這將做到這一點:

$status = false; 
$name = ''; 

if (!empty($_POST['nurse_forename'])) { 
    $name = $_POST['nurse_forename']; 
    $status = true; 

} elseif (!empty($_POST['valuelist'])) { 
    $name = $_POST['valuelist']; 
    $status = true; 

} else { 

    $status = false; 
    // none of nurse_forname OR valuelist is filled 
    // abort. 
} 
+0

如果用戶沒有從數據庫中選擇一個Forename(選擇框保留爲空)並且他們需要輸入一個名稱,這是否會工作? – user2075528 2013-02-16 21:55:31

+0

不,這將檢查所有元素(在$ req中)是否爲空。如果您想檢查是否提交了「valuelist」或「nurse_forename」,則需要對其進行更改。但是您的HTML表單不是爲此設計的,您還需要更改它。 – nepda 2013-02-16 22:12:57

+0

這是如何實現的? – user2075528 2013-02-16 22:35:27

0

if($_POST['valuelist'] == NULL or $_POST['nurse_forename'] == NULL){ die('empty'); }

未經檢驗。

0

喜歡的東西

foreach($_POST as $form_entry) 
if(empty($form_entry)) 
    echo 'you have to fill in all fields'; 
0

試試這樣說:

if(empty($_POST['nurse_forename'])){ 
echo "Field Nurse-Forename is empty"; 
} 

你也可以查看這樣的:

if($_POST['nurse_forename']==""){ 
echo "Nurse-Forename is empty"; 
} 

你不能檢查與一個命令所有字段(因爲你不能區分一個或多個空字段)。你可以使用面向對象更優雅一些,但我認爲你在上面發佈的代碼應該這樣做。

+0

這會工作,如果用戶沒有從數據庫中選擇一個用名字(選擇留空白框),他們需要輸入一個名稱來代替? – user2075528 2013-02-16 21:51:43

+0

是的,如果你單獨檢查每個字段,它將起作用。你將不得不與操作員一起玩,並檢查兩個字段是如何發送的。或者你給這兩個領域相同的名稱。 – BNetz 2013-02-17 00:17:43

0
if (isset($_POST['variable']{0})) { 
    echo 'I exist and I have at least one char!'; 
    else 
    echo 'I dont exist or I have no chars!'; 

它檢查$_POST['variable']是否存在並且至少有一個字符。

0

,你也可以試試這個,這是驗證所有的表單項目。

if (isset ($_POST ['submit_button_name'])) { 
    $validated = true; 
    array_walk_recursive ($_POST, function ($value, $key) { 
     global $validated; 
     if (! trim ($value)) 
      $validated = false; 
    }); 
    if ($validated) { 
     // insert function and redirect 
    } else { 
     // print Your message 
    } 
} 
// Your form 
0
<!DOCTYPE HTML> 
<html> 
<head> 
<style> 
.error {color: #FF0000;} 
</style> 
</head> 
<body> 

<?php 
// define variables and set to empty values 
$nameErr = $emailErr = $genderErr = $websiteErr = ""; 
$name = $email = $gender = $comment = $website = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") 
{ 

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

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

    if (empty($_POST["website"])) 
    {$website = "";} 
    else 
    {$website = test_input($_POST["website"]);} 

    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; 
} 
?> 

<h2>PHP Form Validation Example</h2> 
<p><span class="error">* required field.</span></p> 
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
    Name: <input type="text" name="name"> 
    <span class="error">* <?php echo $nameErr;?></span> 
    <br><br> 
    E-mail: <input type="text" name="email"> 
    <span class="error">* <?php echo $emailErr;?></span> 
    <br><br> 
    Website: <input type="text" name="website"> 
    <span class="error"><?php echo $websiteErr;?></span> 
    <br><br> 
    Comment: <textarea name="comment" rows="5" cols="40"></textarea> 
    <br><br> 
    Gender: 
    <input type="radio" name="gender" value="female">Female 
    <input type="radio" name="gender" value="male">Male 
    <span class="error">* <?php echo $genderErr;?></span> 
    <br><br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 

<?php 
echo "<h2>Your Input:</h2>"; 
echo $name; 
echo "<br>"; 
echo $email; 
echo "<br>"; 
echo $website; 
echo "<br>"; 
echo $comment; 
echo "<br>"; 
echo $gender; 
?> 

</body> 
</html>