skip to Main Content

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>&nbsp;</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


  1. 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 ($_SERVER["REQUEST_METHOD"] == "POST") {
        if(empty($_POST['page_title'])){
            $titleerror = "Title is required";
            echo $titleerror; /*error part-1 */
    
    
        }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"; /*error part-2 */
    
                }
        }
    
    $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']);
    }
    

    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…

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if(empty($_POST['page_title'])){
            $titleerror = "Title is required";
            new Redirect('../index.php?error="The page need a page title"'); 
    
    
        }elseif
        {
            $page_title=mysqli_real_escape_string($objPage->conxn,$_POST['page_title']);
        if (!preg_match("/^[a-zA-Z ]*$/",$page_title)) {
            new Redirect('../index.php?error="Only letters and white space allowed"'); 
    
                }
        }
    else {
    $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']);
         }
    }
    
    Login or Signup to reply.
  2. I Think, this is the better way to Check validation. Add onsubmit in form. Check CheckValidation() function in tag.

    <form name="form1" method="post" action="process/process_add_page.php" onsubmit="return CheckValidation()">
        <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" style="display:none" id="RequiredTitle">* Title is required</span>
                <span style="color:#FF0000" style="display:none" id="OnlyLetters">* Only letters and white space allowed</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>
            .
            .
            .
            //Rest of the code
    </form>
    
    <script>
        function CheckValidation()
        {
            var Title=$('#page_title').val();
            if(Title=="")
            {
                $('#RequiredTitle').show();
                return false;
            }
            else
            {
                if(!preg_match("/^[a-zA-Z ]*$/",Title))
                {
                    $('#OnlyLetters').show();
                    return false;
                }
            }
        }
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search