0

I have a form that has action="file.php" , That PHP file has the cheeks and outputs , I want these outputs to be on the same form page.

The form exists in an html file :

 <form class="" method="post" action="file.php">
    <!-- Some Form Fields -->
</form>

The file.php contains some checks and outputs like:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    if ( empty($name){
        echo "Name can't be empty.";
    }
}

How to show these outputs on the same page next to the form?

8
  • 2
    You have to either use javascript to validate the form before sending it or ajax Commented May 31, 2018 at 15:58
  • Use the required attribute in the form field and the browser will do it automatically.
    – Barmar
    Commented May 31, 2018 at 15:59
  • @Barmar , But if the user removed these attributes , Then he can bypass it
    – Frank
    Commented May 31, 2018 at 16:01
  • 1
    If you wanna do it with PHP, you have to use ajax, there's no other way. Commented May 31, 2018 at 16:02
  • Is your form also on file.php? or you are posting to different page?
    – Andrew
    Commented May 31, 2018 at 16:02

3 Answers 3

1

If you are posting to the same page, you can echo errors on the same page & retain the posted data no problem with something like this.

in php

   $errors = []
   if (empty($name){
        $errors['name'] = "Name can't be empty.";
    }

... and on the form

<php if (isset($errors['name'])) echo $errors['name']; ?>

<input name='name' value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>"></input> 

If you are posting to a different url, then you will have to redirect back to the form page or include the form page, and pass the $errors and the $_POST data.

0

Do everything in the same file.php:

<form class="" method="post" action="file.php">
    <!-- Some Form Fields -->
</form>
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    if ( empty($name){
        echo "Name can't be empty.";
    }
}
0

If you're trying to have that validation in the same form file, let's call it 'form.html', first rename it to 'form.php'. Then add the <?php ?> tags above all the html code, and write your code there.

For that to work, you need to have to left empty the action attribute in form

   <?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $name = $_POST['name'];
        if ( empty($name){
            echo "Name can't be empty.";
        }
    }
    ?>
    <!-- Some HTML Code-->
    <form class="" method="post" action="">
        <!-- Some Form Fields -->
    </form>

I hope this answer your question about having the code in the same page, but if what you're trying to do is check if field is empty, i recommend you use the 'required' attribute in the html form's input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.