Typ...">

Embed php in html to keep text in textbox after user submit value

<html>
<body>

<form action="http://localhost/index1.php" method="post">
    Type your name: <input type="text" name="name" value="<?php echo $_POST['name'];?>">
                    <input type="submit" value="Submit">
</form>

</body>
</html>

      

What I was trying to do was save the data entered in the textbox after the user entered a value and clicked the submit button.

When I click on the submit button, it gives a notification in the textbox saying that the name is undefined which I understand why. $ _POST ['name'] doesn't matter.

I'm just wondering if there is a way to do this. I am starting with php and HTML.

+3


source to share


5 answers


<?php
//check whether the variable $_POST['name'] exists
//this condition will be false for the loading
if(isset($_POST['name']){
  $name = $_POST['name'];
}
else{
  //if the $_POST['name'] is not set
  $name = '';
}


?>

<html>
<body>

<form action="http://localhost/index1.php" method="post">
    Type your name: <input type="text" name="name" value="<?php echo $name;?>">
                    <input type="submit" value="Submit">
</form>

</body>
</html>

      



+6


source


Try this (assuming it is index1.php

and you are using UTF-8

for your content type)



<?php
$name = isset($_POST['name']) ? $_POST['name'] : null;
?>

<!-- your HTML, form, etc -->

<input type="text" name="name"
    value="<?php echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>">

      

+3


source


From @Phil's answer, given that using "isset" is not a solution ( EDIT : I'm wrong on this question, see comments), the correct function is to check $ _POST as the "name" of the input you are trying to get:

<?php
$name = null;
if (array_key_exists('name', $_POST)) {
    $name = $_POST['name'];
}
//more condensed : $name = array_key_exists('name', $_POST) ? $_POST['name'] : null;
?>

<!-- your HTML, form, etc -->

<input type="text" name="name"
    value="<?php echo htmlspecialchars($name, ENT_QUOTES, 'UTF-8') ?>">

      

The message that appears is a PHP warning reading about your error_reporting error in PHP might be a good idea.

+1


source


@Dallas

You are on the right track; here is an example (very rough and dirty) that displays the field value of the submitted POST in its original textarea

<form action="thispage.php" method="post">
    <input type="text" name="mytext" value="<?php echo isset($_POST['mytext'])?$_POST['mytext']:''?>" />
    <input type="submit" value="submit"/>
</form>

      

This means that a POST field is defined mytext

, and (if so) returns that value in the text field. If it is not defined, it just gives an empty value.

If it doesn't work for you, I will double check name

your textbox attribute against the key value you are looking for in $ _POST.

0


source


Since your action attribute points to index1.php, I assume the given HTML is in the same index1.php, in which case you should do this

<html>
<body>

<form action="http://localhost/index1.php" method="post">
    Type your name: input type="text" name="name" value="<?php if(isset($_POST['name'])){echo $_POST['name'];}?>
                    input type="submit" value="Submit">
</form>

</body>
</html>

      

<?php echo isset($_POST['name'])?$_POST['name']:''?>

is another way to make the value attribute conditional

0


source







All Articles