2013-06-24 45 views
0

我對PHP很新,我似乎無法找到我在找什麼。 我希望在填寫表單的頁面上顯示錯誤。 我想要一個「請輸入名稱」錯誤,以顯示它是否在表單頁面的$ showerror部分中爲空。窗體上的PHP表單錯誤消息頁面

這是我迄今爲止...

<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. [email protected]"></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 = '[email protected]'; 
$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!."; 
    } 
?> 
+0

你確定你不是在尋找一個Javascript解決方案嗎? – Christoffer

+0

這$淋浴錯誤是填充何時何地? – Aris

+0

JavaScript更簡單,更高效嗎? – user2515606

回答

2

您需要將PHP標籤之間的PHP語法,例如,該行:

<td colspan="2">$showerror</td> 

變成這樣:

<td colspan="2"><?php echo $showerror; ?></td> 

如果你是完全新的PHP,你可以從一些教程網站開始學習,這裏有一個good one

編輯:

您可以在PHP頁面中設置$ showerror,這可能是每個表單字段的「if條件」的長列表,但我會告訴您一個小名稱/簡單示例,用於全名$_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'; 
} 
+0

雖然我不確定如何編寫$ showerror代碼。 – user2515606

+0

@ user2515606我編輯了我的答案,希望對您有所幫助 – evilReiko

+0

無論我是否輸入或不輸入「姓名」的形式,只是說「謝謝,我們已收到您的詢問。」只要我輸入反垃圾郵件 – user2515606

2

編譯器只解析爲被<?php ?>標籤之間寫PHP的內容,以便使用

<td colspan="2"><?php echo $showerror; ?></td> 

<td colspan="2"><?= $showerror ?></td> 
0

試試這個,

<?php 
    if(isset($showerror)) 
     echo $showerror; 
    else 
     echo ''; 
    ?> 

,如果你沒有在驗證頁面

0

的第一彙編$showerror變量代碼試試這個,你會得到一個錯誤信息Undefined variable ..你必須將這兩個代碼放在同一個文件中。 並且可以檢查請求是否只發布後讀取php。 然後你可以把價值放在$ sho