Jump to content

How to display error messages next to form inputs when they are empty, and remove error messages when inputs filled?


Recommended Posts

How is it possible, in PHP, to display an error message next to a form input text field if a user is attempting to submit a form with empty fields? Moreover, how is it possible to remove the same error message when the user fills in the input field with the required data and/or refreshes the page?

I have 2 files that I am working with: application.php and process.php.

application.php mainly has the HTML of the form.
process.php has the PHP for processing and validating the form.

I am very new to learning PHP (I started learning it last week) and I have been searching for hours for a clear answer. I have already tried different methods for generating the error message including: using empty(), !isset, $_POST["name"] = "", etc, and have tried using session_start();, $row, echo, print, and other variables to try and display error message on the page, and I have tried using unset();, and = null, to try and remove the error message once the input field has been filled but all to no avail. Either the method I try only half works, or nothing works, and I cannot pinpoint which is the part that is not working.

I only have 2 files to work with, and as an example of what I want to do is:

1. If the first name field is empty when the user clicks submit, an error message should appear next to the input.
2. If the user fills in the input and clicks submit, the form should be processed and added to the database.
3. When the user returns to the application to page to fill in another application form, the error message should not appear.

Is this possible with PHP?

Link to post
Share on other sites

here's a list of practices that will result in a web page that does what you are asking, with a minimum of code  -

1. put the form processing code and the form on the same page. the form processing code goes above the start of the html document. the form processing code needs to detect if a post method form was submitted before executing any of its code.

2. in the form processing code, store validation error messages in an array, with the form field name as the array key.

3. after the end of the validation logic, if the array holding the validation error messages is empty, there are no errors and you can use the submitted data.

4. after successfully processing the form data, execute a redirect to the exact same URL of the current page to cause a get request. this will prevent the browser from trying to resubmit the form data if you refresh the page or browse back to the url of the page. this will also cause a blank form, with no error messages to be displayed.

5. at the point of (re)displaying the form fields, you would test if the element in the errors array matching the current field name isset() and output the error message. you would also test for and output the existing field value so that the user doesn't need to keep reentering the data over and over, just correct the errors in the existing data.

Link to post
Share on other sites

Hi mac_gyver. Thank you for such a detailed and helpful response. Unfortunately, it is for an assignment and I am not allowed to use php in the same file as the HTML code (not significantly anyway, it has to be very minimal php in the HTML file, such as echo and include). I have to use 2 separate files: application.php (that contains mostly HTML) and processing.php (which has mostly PHP).

I have tried the "session_start" method, and I can get it to "kind of" work, but I when the page refreshes or the input is filled, the previous error message is still there, persistently, and will not go away. I deleted that method and tried other methods, to no avail.

Do you know the correct method of how to link 2 pages and get the error message (from processing.php) to appear next to the input field (in application.php), but also be removed upon page refresh or a new application submission?

Link to post
Share on other sites

just because you have two files, doesn't necessarily mean they cannot be used on the same page. just 'require' the file containing the html/minimal amount of php code, at the appropriate place to produce a single page with all the functionality on it. :thumb-up:

if you don't think your instructor would go for that distinction, you would need to store the validation errors in a session array variable and also store the submitted array of post data in a session variable so that when you redirect back to the form page you can use the data in the same way as suggested above. all page(s) that set or use session variables must have a session_start() statement on it. located before anything is output to the browser.

Link to post
Share on other sites
This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.