How to insert $_POST in HTML form input?

Asked

Viewed 1,445 times

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...

  • 1

    @Florida thank you. This way is now?

  • 1

    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 use ajax

  • @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.

  • 1

    If you can enable or disable a resource, it loses the title of validation. Never treat an optional user resource as a security tool.

2 answers

2


use the function filter_input() within the attribute 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 by checking to avoid errors of the function itself.

Important: do not put the entered password in the form because it can expose the user data.

  • exactly that! Thanks for the solution!

1

Hello no need to even write php for this, just put the fields require="" that it is an attribute of HTML 5 so if there is no data inserted or will pass 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 believe that it works and will keep the code much more succinct, now if you do not want to do so you can create session variables or put in GET or POST variables directing to the same page, an example of GET variable would be:

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

?>

<input value="<?=$nome;?>" type="text" name="nome">
  • Failed to mention what required and an attribute of HTML5

  • @André Martins the role require HTML5 is functional in older browser versions? I was avoiding using it because I read that it was not very reliable. It would not be necessary to use sessions, because the way I did the data would be sent to the same page, but how would I insert the data into the HTML input? Where should I put <?=variavelSessao?> for him to fill in the input?

  • 1

    edited response to meet you @Luan

  • 2

    nor will it pass? Browser-independent Test This: Click Inspect Element, Find Input, Remove Useless require, submit the form and you will see a form being submitted.

  • @Papacharlie don’t get trauma in boy require is good but bad are browsers like IE 6 and his family.

  • 1

    @Andremartins excuse ignorance? but how can you say that the navigators of all the internet are bad? Your solution is effective but not efficient

  • I have instructed how to 'bypass' the functionality of require And you claim it’s the ancient navigators' fault? Your answer does not provide a real solution, and I am avoiding to deny your answer without you becoming aware of the error that was your statement.

  • @Papacharlie in fact, I read some related articles and saw that the require is not reliable. @Andrémartins thanks for the solution presented. It gave me an overview of how it works.

  • @What Papacharlie meant was that there is no silver bullet and he can’t just say that require is useless.

  • @Marcelobonifazio mentioned only IE and his family, now if a web developer says IE 6 is good, there’s something wrong.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.