2
How to insert a $_POST
in a input
of a form HTML
?
In a simple login form, when leaving a blank field, the script rescues the filled field, keeps it filled, and informs the empty field error, so that the user does not have to type again.
There’s a way to do this with PHP
, without Javascript
?
Basically the script would receive the $_POST
sent and filled in the respective field.
Only the insertion script is missing anyway.
<?php
function checkUsername($username)
{
if ($username == "")
{
echo "<span style='color:red'>Preencha o campo Username!</span><br>";
}
}
function checkPassword($password)
{
if ($password == "")
{
echo "<span style='color:red'>Preencha o campo Password!</span><br>";
}
}
<?php
include 'includes/functions.php';
$check = false;
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
checkUsername($_POST['username']);
checkPassword($_POST['password']);
$check = true;
}
?>
<!-- FORMULÁRIO DE LOGIN -->
<div id="form">
<form action="" method="post">
<div class="_100">
<label for="username">Username: </label><br>
<input type="text" name="username" placeholder="Username"><br>
</div>
<div class="">
<label for="password">Password: </label><br>
<input type="password" name="password" placeholder="*******"><br>
</div>
<br>
<div class="">
<input type="submit" value="Entrar no Sistema">
</div>
</form>
</div>
<!-- /FORMULÁRIO DE LOGIN -->
Add to formatting in the
HTML
,PHP
,Javascript
and$_POST
to be more organized, my edition conflicted with your...– Florida
@Florida thank you. This way is now?
– Luan Vicente
to do what you want, from a study in
ajax
, the ideal would be to do this client-side validation(JS), but if it is ESSENTIAL to do the server-side validation(PHP), the ideal and useajax
– MarceloBoni
@Marcelobonifazio I am preparing a project to practice what I have been studying in a self-taught way, so the confusion on how to do and what to use for such. Thank you, I’ll study yes.
– Luan Vicente
If you can enable or disable a resource, it loses the title of validation. Never treat an optional user resource as a security tool.
– Papa Charlie