我有檢查三個字段,姓名,有效的電子郵件和評論的電子郵件的形式。但現在它的設置方式,因爲名稱和註釋在一個函數中,它首先檢查名稱和註釋,即使電子郵件無效,我如何重新編寫它,以便按順序檢查字段。另外,我想重新顯示沒有錯誤的字段,因此用戶不必再次輸入。請幫忙。由於爲了和重新顯示正確的字段PHP顯示錯誤消息
<?php
$myemail = "[email protected]";
$yourname = check_input($_POST['yourname'], "Enter your name!");
$email = check_input($_POST['email']);
$phone = check_input($_POST['phone']);
$subject = check_input($_POST['subject']);
$comments = check_input($_POST['comments'], "Write your comments!");
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Enter a valid E-mail address!");
}
exit();
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<!doctype html>
<html>
<body>
<form action="myform.php" method="post">
<p style="color: red;"><b>Please correct the following error:</b><br />
<?php echo $myError; ?></p>
<p>Name: <input type="text" name="yourname" /></P>
<P>Email: <input type="text" name="email" /></p>
<P>Phone: <input type="text" name="phone" /></p><br />
<P>Subject: <input type="text" style="width:75%;" name="subject" /></p>
<p>Comments:<br />
<textarea name="comments" rows="10" cols="50" style="width: 100%;"></textarea></p>
<p><input type="submit" value="Submit"></p>
</form>
</body>
</html>
<?php
exit();
}
?>
我試圖把正則表達式到check_input功能,但隨後的電子郵件錯誤,無論表現。 – Mosaic 2012-04-18 23:11:13