How to insert $ _POST into HTML form input?

2

How can I insert a $_POST into a input of a HTML form? In a simple login form, by leaving a field blank, the script retrieves the filled field, keeps it filled, and reports the error of the empty field, so the user does not have to type again.

Is there a way to do this with PHP , without Javascript ? Basically the script would receive the $_POST sent and fill the respective field.

Only the same insertion script is missing.

<?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 -->
    
asked by anonymous 17.06.2015 / 03:48

2 answers

2

use the filter_input() function within the value :

<input type="text" name="username" placeholder="Username" value="<?php
  echo filter_input('username', INPUT_POST);
?>">
<input type="password" name="password" placeholder="*******"/>

The code is equivalent to echo $_POST['username'] , except for the checks to avoid errors of the function itself.

Important: Do not put the entered password in the form as it can expose user data.

    
17.06.2015 / 14:38
1

Hello, you do not even have to write php for this, just put the fields require="" which is an HTML 5 attribute so if there is not a data entered or it will be clear depending on the browser version:

<div id="form">
<form action="" method="post">
    <div class="_100">
        <label for="username">Username: </label><br>
        <input type="text" name="username" placeholder="Username" require=""><br>
    </div>
    <div class="">
        <label for="password" require="">Password: </label><br>
        <input type="password" name="password" placeholder="*******" require=""><br>
    </div>
    <br>
    <div class="">  
        <input type="submit" value="Entrar no Sistema">
    </div>
</form>

I think it works and it will be much more succinct, so if you do not want to do this, you can create session variables or put GET or POST variables in the same page. >

<?php
if(isset($_GET["nome"])) {
    $nome   = $_GET["nome"];
} else {
   nome="";
}
  //e em seu input

?>

<input value="<?=$nome;?>" type="text" name="nome">
    
17.06.2015 / 04:53