I think this is very simple but I don’t know how to execute it.
I have a form with some data and I have created a another php file to validate the form data, but I am unable to display the error message back into the form if validation fails. I have attached both files, but I don’t know hot to execute it 🙁
My form.php looks like
<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>
and my process_add_pages.php looks like this
<?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');
}
?>
2
Answers
There are few little changes you have to do to make it work..
There is a couple of problem in your process_add_pages.php file. your code is ..
if anything goes wrong in the validation it will echo the error message in the process_add_pages.php file. but it will also execute the sql queries. so if there is any problem in validation you can redirect to the form page with an error message.
you can try this new code format…
I Think, this is the better way to Check validation. Add onsubmit in form. Check CheckValidation() function in tag.