我認爲這很簡單,但我不知道如何執行它。php表單驗證和顯示錯誤
我有一個表格,有一些數據,我創建了另一個php文件來驗證表單數據,但是如果驗證失敗,我無法將錯誤信息顯示回表單。我重視這兩個文件,但我不知道燙去執行它:(
我form.php的樣子
<form name="form1" method="post" action="process/process_add_page.php">
<fieldset>
<legend>Add Page</legend>
<table width="1056" height="365" border="1">
<tr>
<th width="77" scope="col">Page Title</th>
<th width="962" scope="col"><label for="page_title"></label>
<input type="text" name="page_title" id="page_title"><span style="color:#FF0000">* <?php echo $titleerror;?></span></th>
</tr>
<tr>
<th scope="row">Page Description</th>
<td><label for="page_description"></label>
<textarea name="page_description" class="ckeditor" id="page_description" cols="100" rows="5"></textarea></td>
</tr>
<tr>
<th scope="row">Seo Title</th>
<td><label for="seo_title"></label>
<input type="text" name="seo_title" id="seo_title"></td>
</tr>
<tr>
<th scope="row">Seo Description</th>
<td><label for="seo_description"></label>
<textarea name="seo_description" class="ckeditor" id="seo_description" cols="45" rows="5"></textarea></td>
</tr>
<tr>
<th scope="row">Page Order</th>
<td><label for="page_order"></label>
<input type="text" name="page_order" id="page_order"></td>
</tr>
<tr>
<th scope="row">Page Status</th>
<td><label for="page_status"></label>
<select name="page_status" id="page_status">
<option value="1">Active</option>
<option value="0">Inactive</option>
</select></td>
</tr>
<tr>
<th colspan="2" scope="row"><input type="submit" name="btnsubmit" id="btnsubmit" value="Submit"></th>
</tr>
</table>
<p> </p>
</fieldset>
</form>
和我process_add_pages.php看起來像這樣
<?php
require_once('../../classes/database.php');
require_once('../../classes/pages.php');
require_once('../../classes/redirect.php');
$objPage=new Page();
$titleerror='';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if(empty($_POST['page_title'])){
$titleerror = "Title is required";
echo $titleerror;
}else
{
$page_title=mysqli_real_escape_string($objPage->conxn,$_POST['page_title']);
if (!preg_match("/^[a-zA-Z ]*$/",$page_title)) {
$titleerror = "Only letters and white space allowed";
}
}
$page_description=mysqli_real_escape_string($objPage- >conxn,$_POST['page_description']);
$seo_title=mysqli_real_escape_string($objPage->conxn,$_POST['seo_title']);
$seo_description=mysqli_real_escape_string($objPage->conxn,$_POST['seo_description']);
$page_order=mysqli_real_escape_string($objPage->conxn,$_POST['page_order']);
$page_status=mysqli_real_escape_string($objPage->conxn,$_POST['page_status']);
}
$objPage->setPage_title($page_title);
$objPage->setPage_description($page_description);
$objPage->setSeo_title($seo_title);
$objPage->setSeo_description($seo_description);
$objPage->setPage_status($page_status);
if($objPage->addPage()){
new Redirect('../index.php?page=page&action=view&success=The page has been created');
}else{
new Redirect('../index.php?page=page&action=view&error=The page could not be created');
}
?>
你爲什麼使用兩個分離的文件? –
因爲我在管理代碼的onle文件夾中都有表格處理文件 –