2011-02-09 28 views
1
<?php 
$name = $_POST["name"]; 
$email = $_POST["email"]; 
$gender = $_POST["gen"]; 
$age = $_POST["age"]; 
$comments = $_POST["comments"]; 

if(empty($_POST["name"])){ 
echo "empty name"; 
} 
if(empty($_POST["email"])){ 
echo "empty email"; 
} 

/// 
if(empty($_POST["gen"])){ 
echo "empty gender"; 
} 
if(empty($_POST["comments"])){ 
echo "empty comments"; 
} 

if(!isset($_POST['submit'])) { 
?> 

<html> 
<head> 
<title>Lab6 : P1</title> 
</head> 

<body> 
<fieldset> 
<legend><h4>Enter your information in the fields below</h4></legend> 
<form name="info" method="post" action="<?php echo $PHP_SELF;?>" method="post"> 
<strong>Name:</strong> <input type="text" name="name" id="name" /><br> 
<strong>Email:</strong> <input type="text" name="email" id="email" /><br> 
<br> 
<strong>Gender</strong><br> 
<input type="radio" name="gen" value="Male">Male</input><br> 
<input type="radio" name="gen" value="Female">Female</input><br> 
<br> 
<select name="age"> 
<option value="Under 30">Under 30</option><br> 
<option value="Between 30 and 60">Between 30 and 60</option><br> 
<option value="60+">60+</option> 
</select> 
<br> 
Comments: <textarea name="comments" cols="20" rows="5"> </textarea> 
</fieldset> 
<input type="submit" name="submit" value="Submit my Information" /> 
</form> 
</body> 
</html> 
<? 
} 

else { 
echo "Thank you, ".$name." for your comments: " . "<strong>" . $comments . "</strong>"; 
echo "<br>We will reply to you at:" . "<em>" . $email . "</em>"; 
} 

?> 

我需要它來驗證字段:名稱,電子郵件,空字符串的註釋(不允許),並且不允許未選定的單選按鈕用於性別和年齡選擇。驗證空/無線電字段 - Php

誰能幫

+0

你似乎已經這樣做了(在某種程度上)。你需要什麼特別的幫助? – deceze 2011-02-09 03:27:05

回答

2
<?php 

// to eventually re-fill the fields 
$name = ""; 
$email = ""; 
$gender = ""; 
$age = ""; 
$comments = ""; 

// to re-select a radio button and select option 
$Mchecked = ""; 
$Fchecked = ""; 
$selectMinus_30=""; 
$select30_to_60=""; 
$select60_plus=""; 

// to display errors 
$error = ""; 

$done=false; 

if (isset($_POST["name"]) && isset($_POST["email"]) && isset($_POST["age"])){ 
    if($_POST["name"]==""){ 
    $error = "empty name <br/>"; 
    } 

    if($_POST["email"]==""){ 
    $error = $error . "empty mail <br/>"; 
    } 

    if(!isset($_POST["gen"])){ 
    $error = $error . "empty gender <br/>"; 
    } 
    else{ 
     $gender = $_POST["gen"]; 
     if ($gender == "Male"){ 
      $Mchecked = "checked"; 
     } 
     else if ($gender == "Female"){ 
      $Fchecked = "checked"; 
     } 
    } 

    if($_POST["comments"]==""){ 
    $error = $error . "error: empty comments <br/>"; 
    } 


    $name = $_POST["name"]; 
    $email = $_POST["email"]; 
    $comments = $_POST["comments"]; 

    $age = $_POST["age"]; 
    if ($age == "Under 30"){ 
     $selectMinus_30 = "selected"; 
    } 
    else if ($age == "Between 30 and 60"){ 
     $select30_to_60 = "selected"; 
    } 
    else if ($age == "60+"){ 
     $select60_plus = "selected"; 
    } 

    if ($error==""){ 
     $done=true; 
    } 
} 
?> 

<html> 
<head> 
<title>Lab6 : P1</title> 
</head> 

<body> 

<?php if (!$done){ ?> 
    <fieldset> 
    <legend><h4>Enter your information in the fields below</h4></legend> 
    <p class="error" style="color:red;"><?php echo $error;?></p> 
    <form name="info" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" method="post"> 
    <strong>Name:</strong> <input type="text" name="name" id="name" value="<?php echo $name; ?>" /><br/> 
    <strong>Email:</strong> <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br/> 
    <br/> 
    <strong>Gender</strong><br/> 
    <input type="radio" name="gen" value="Male" <?php echo $Mchecked;?>>Male</input><br/> 
    <input type="radio" name="gen" value="Female" <?php echo $Fchecked;?>>Female</input><br/> 
    <br/> 
    <select name="age" value="<?php echo $age;?>"> 
    <option value="Under 30" <?php echo $selectMinus_30;?>>Under 30</option><br/> 
    <option value="Between 30 and 60" <?php echo $select30_to_60;?>>Between 30 and 60</option><br/> 
    <option value="60+" <?php echo $select60_plus;?>>60+</option> 
    </select> 
    <br/> 
    Comments: <textarea name="comments" cols="20" rows="5"><?php echo $comments; ?></textarea> 
    </fieldset> 
    <input type="submit" name="submit" value="Submit my Information" /> 
    </form> 
<?php }else{ 
    echo "Thank you, ".$name." for your comments: " . "<strong>" . $comments . "</strong>"; 
    echo "<br/>We will reply to you at:" . "<em>" . $email . "</em>"; 
} ?> 

</body> 
</html> 

在這裏,我做了適當的驗證,以及當你與任何空字段提交重新填寫表格。


P.S.謝謝Marc B,我沒有意識到,我的第一篇文章很有趣。

+0

這將是一個非常低效的方法。如果整個表單發送空缺,您只會在表單提交時收到一條錯誤消息。 「沒有名字」[填寫姓名]「沒有電子郵件」[填寫電子郵件]等等......同樣,你沒有填寫以前提交的值,所以用戶每次都必須從頭開始。 – 2011-02-09 03:59:34