html - PHP error displaying position.
Get the solution ↓↓↓I have following PHP code:
if($_SESSION['msg']['login-err'])
{
echo '<p class="error">'.$_SESSION['msg']['login-err'].'</p>';
unset($_SESSION['msg']['login-err']);
}
Everything working fine. 'P' with error is displayed correctly inside element.
My question is how to specify where my error should be displayed?
E.G. Not as the first element inside the form, but
- as the second for example
- just before
<input name="user" />
??
Any suggestion much appreciated.
Answer
Solution:
Since you're (somewhat naively) testing for errors and displaying errors at the same time, you should just move the code lower in the page so that the test/output happen where you want it to display.
Ideally you should decouple your display code from your logic. Store your errors for later display in an$errors
variable which is passed to your template to be rendered.
Answer
Solution:
Rather than placing the echo where it exist now, why not change your code to something like this:
$error;
if($_SESSION['msg']['login-err'])
{
$error = $_SESSION['msg']['login-err'];
unset($_SESSION['msg']['login-err']);
}
Then, right above your input tag, do something like this...
<?php echo $error;?>
It's not the best way to do error handling...but it should do what you are asking.
Answer
Solution:
The "where" depends on you frontend design.
In a login form I would recommend to display errors at the top or bottom of the whole form. In a contact/registration form it makes more sense to show error beside the field ( on the top or left ).
Quite different thing is this combination of logic and presentations. They should be separated, because each piece of code should have only one reason to change. Your piece has two reasons: design or logic.
You would gain a lot if you did some extended studies in MVC and OOP in general.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: your lock file does not contain a compatible set of packages. please run composer update.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.