[英]PHP Form Error Message on Form Page
我對PHP還是很陌生,但似乎找不到我想要的東西。 我希望錯誤顯示在填寫表單的頁面上。 我希望在表單頁面的$ showerror部分顯示“請輸入名稱”錯誤。
這是我到目前為止所擁有的...
<form method="post" action="process.php">
<table width="667">
<td colspan="2"><?php echo $showerror; ?></td>
<tr>
<td>Full Name:*</td>
<td><input class="text" name="name" placeholder="Ex. John Smith"></td>
<tr>
<td>E-mail:*</td>
<td><input class="text" name="email" placeholder="Ex. johnsmith@gmail.com"></td>
<tr>
<td>Type of site:*</td>
<td>Business<input name="multioption[]" type="radio" value="Business" class="check" /> Portfolio<input name="multioption[]" type="radio" value="Portfolio" class="check" /> Forum<input name="multioption[]" type="radio" value="Forum" class="check" /> Blog<input name="multioption[]" type="radio" value="Blog" class="check" /></td>
<tr>
<td>Anti-Spam: (2)+2=?*</td>
<td><input name="human" placeholder="What is it?"></td>
</table>
<input id="submit" name="submit" type="submit" value="Submit"></form>
這就是我的process.php的樣子...我不確定如何編碼錯誤部分。
<?php
$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$human = isset($_POST['human']) ? $_POST['human'] : '';
$submit = isset($_POST['submit']) ? true : false;
$multioption = isset($_POST['multioption'])
? implode(', ', $_POST['multioption'])
: 'No multioption option selected.';
$from = 'From: Testing Form';
$to = 'xx@xxxxx.com';
$subject = 'Testing Form';
$body =
"Name: $name\n
E-Mail: $email\n
Multi Options: $multioption\n";
if ($submit && $human == '4') {
mail ($to, $subject, $body, $from);
print ("Thank you. We have received your inquiry.");
}
else {
echo "We have detected you are a robot!.";
}
?>
您需要在PHP標記之間放置PHP語法,例如,以下行:
<td colspan="2">$showerror</td>
變成這樣:
<td colspan="2"><?php echo $showerror; ?></td>
如果您完全不熟悉PHP,則可以從一些教程網站開始學習,這是一個不錯的選擇
編輯:
您可以在PHP頁面中設置$ showerror,這可能是每個表單字段的一長串“如果條件”,但是我將向您展示一個小/簡單的全名$_POST['name']
示例,它將是這樣的:
$showerror = '';
if(!empty($_POST['name'])) {// enter here if fullname is set
if(strlen($_POST['name']) >= 6 && strlen($_POST['name']) <= 12) {// enter here if fullname length is between 6-12 characters
// You can do more validation by using "if conditions" here if you would like, or keep it empty if you think fullname is correct
} else {// enter here if fullname is NOT between 6-12 characters
$showerror = 'Full name must be 6-12 characters';
}
} else {// enter here if fullname is not set
$showerror = 'Please enter your full name';
}
編譯器僅將內容解析為<?php ?>
標記之間編寫的php,因此使用
<td colspan="2"><?php echo $showerror; ?></td>
要么
<td colspan="2"><?= $showerror ?></td>
嘗試這個,
<?php
if(isset($showerror))
echo $showerror;
else
echo '';
?>
如果您沒有在$showerror
的第一次編譯中驗證$showerror
變量的代碼,則會收到錯誤消息Undefined variable
試試這個..您必須將兩個代碼都放在同一個文件中。 並可以檢查請求是否發布僅讀取php。 然后可以將值放入$ sho
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.