1

I have to display a server-side validation error message which is working perfectly if I write both codes(HTML and PHP) on a single page.

Now I have index.php and process.php pages. I have to pass server-side validation error message from process.php to index.php. Without using Ajax. would you help me in this?

index.php

<?php

include('../../db/connection.php');
$fname_error="";
$email_error="";
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form action="process.php" method="post">
        <input type="text" name="fname" value="<?php if(isset($fname)){echo $fname;} ?>">
         <span class="error"><?php echo $fname_error;?></span>

        <input type="email" name="email" value="<?php if(isset($email)){echo $email;}?>">
        <span class="error"><?php echo $email_error;?></span>

        <input type="submit" name="submit">
    </form>

</body>
</html>

Process.php

<?php

include('../../db/connection.php');
if (isset($_POST['submit'])) {

    $fname=trim($_POST['fname']);
    $email=trim($_POST['email']);

    if (empty($fname)) {
        $fname_error="Name is empty";
    }

    else
        {
        if ($fname<3) {
        $fname_error="Please enter minimum 3 character";
    }
}

    if (empty($email)) {
        $email_error="Email field is empty";
    }
    else
        {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $email_error="Invalid email format"; 

    }
    }


    // insert code here
}
?>
Naren Verma
  • 2,205
  • 5
  • 38
  • 95

1 Answers1

0

Use SESSIONS to achive this. Try following Code:

index.php

<?php
session_start();
include('../../db/connection.php');
$fname_error= $_SESSION['fname'];
$email_error= $_SESSION['email_error'];
?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form action="process.php" method="post">
        <input type="text" name="fname" value="<?php if(isset($fname)){echo $fname;} ?>">
         <span class="error"><?php echo $fname_error;?></span>

        <input type="email" name="email" value="<?php if(isset($email)){echo $email;}?>">
        <span class="error"><?php echo $email_error;?></span>

        <input type="submit" name="submit">
    </form>

</body>
</html>

<?php
    unset($_SESSION['fname']);
    unset($_SESSION['email_error']);
?>

process.php

<?php
session_start();
include('../../db/connection.php');
if (isset($_POST['submit'])) {

    $fname=trim($_POST['fname']);
    $email=trim($_POST['email']);

    if (empty($fname)) {
        $_SESSION['fname'] ="Name is empty";
        header('location:index.php');
    }

    else
        {
        if ($fname<3) {
        $_SESSION['fname'] ="Please enter minimum 3 character";
        header('location:index.php');
    }
}

    if (empty($email)) {
        $_SESSION['email_error'] ="Email field is empty";
        header('location:index.php');
    }
    else
        {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $_SESSION['email_error']="Invalid email format"; 
          header('location:index.php');

    } 
}


    // insert code here
}
?>
  • Thanks for replying Mr.Hamza, With session is possible. Is there any other way to solve? – Naren Verma Jun 02 '17 at 07:08
  • Other than session you can use GET method. As displaying error via URL is not a good approach, the displaying of paramters via URL is also not good. So i would recommend you to use SESSIONS. This is best and secure. – M.Hamza Mehmood Jun 02 '17 at 07:10
  • Thanks for your information. I need one more information. why you used header() inside if condition? – Naren Verma Jun 02 '17 at 07:12
  • The header('location:index.php'); will redirect the control from process.php back to the index.php. The FORM ACTION took the control to process page, to get back to index page the header() is used. If you find supportive than please upvote :-) – M.Hamza Mehmood Jun 02 '17 at 07:14
  • I am getting error Undefined index: fname and Undefined index: email_error on index page even I declare the variable name empty – Naren Verma Jun 02 '17 at 07:55
  • i forget to mention, add `error_reporting(1);` at the top of both pages. it will hide warnings – M.Hamza Mehmood Jun 02 '17 at 12:05
  • Cool!! Thanks for your help. – Naren Verma Jun 03 '17 at 11:39