Skip to content
Advertisement

Placing Error Message from PHP to specific place in HTML [closed]

I have created a log in system. My PHP is on a separate script file than my HTML. I want to display a specific error message at a specific spot on my login form. How do I do this? Here is my PHP, you will see my error message at the bottom of the code.

JavaScript

Here is my HTML, you will see the error message at the bottom. I want it to be displayed in that cell in the table.

JavaScript

PLEASE NOTE: This is just for an assignment. Not necessary to worry about security

Advertisement

Answer

If the ‘html’ file is an actual plain .html file, you can’t execute php code inside it.

Assuming it is instead a .php file, one possibility is to import the login script before the html part. In this way the $errorMsg variable is visible to the page (you also have to define it as null or empty string outside the else block or check for its existence before trying to output it).
When the page is first loaded, avoid to execute the login block by checking if you are receiving data from the form.

Another possibility, keeping the two scripts separated, is to redirect back to the form page in case of errors. In this case you could either pass the error message (or an error code) as query parameter or save it in the session.
In the first case you check if the GET parameter exists and output it.
In the second case you check if the session key exists, you save it on a local variable or output it, and then unset the session key.

As a side note, I would suggest to use the 303 status code, when a Location redirect is made in response to a POST request.
Example: header("Location: Home.php", true, 303);

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement