Prefilling input text box
<?php // Before PHP 7.0, // use isset function to determine if the field contains value $fname = isset($_POST['fieldname']) ? $_POST['fieldname'] : ''; // For PHP 7.0 or above, use null coalescing operator (??) $fname = $_POST['fieldname'] ?? ''; // Encode html special characters (<, >, ', ", etc.) $fname = htmlspecialchars($fname); ?> ... <input type="text" name="fieldname" value="<?php echo $fname; ?>" > ...
The value in text fields (such as hidden, password, time, datetime-local, color, email, url, number, etc. see form elements) is provided in the value
attribute. However, the data there must be properly encoded with htmlspecialchars()
function to get rid of dangerous characters such as '
or <
or >
. The code snippet in the preceding code expects the form to be submitted back to itself; thus, it extracts the current value of the text field from $_POST
(it would work analogously with $_GET
).
Prefilling Text Fields
<?php $uname = htmlspecialchars ($_POST['uname'] ?? ''); $email = htmlspecialchars ($_POST['email'] ?? ''); ?> <form method="post" action="example.php"> Name:<br> <input type="text" name="uname" value="<?=$uname?>"><br> Email:<br> <input type="email" name="email" value="<?=$email?>"><br> <input type="submit" name="submit" value="Submit"> </form>
If you want to use a default value for this form element, you just have to provide this default value instead of the empty string in the PHP code:
<?php //Guest if uname field is not set $uname = htmlspecialchars ($_POST['uname'] ?? 'Guest'); //no@xy.z if email field is not set $email = htmlspecialchars ($_POST['email'] ?? 'no@xy.z'); ?> <form method="post" action="example.php"> Name:<br> <input type="text" name="uname" value="<?=$uname?>"><br> Email:<br> <input type="email" name="email" value="<?=$email?>"><br> <input type="submit" name="submit" value="Submit"> </form>
Prefilling textarea (multiline text field)
With multiline text fields, almost the same approach as with single-line text fields and password fields can be used. The only difference is the location where to put the prefill value.
To preset the value of a textarea, print the value between the textarea tags:
<textarea cols="40" rows="5" name="areafieldname">Enter your data here ...></textarea>
It belongs between <textarea>
and </textarea>
, as shown in the code.
Prefilling Multiline Text Fields
<?php $mtext = $_POST['mtext'] ?? ''; $mtext = htmlspecialchars($mtext); ?> <form method="post" action="example.php"> Multiline text:<br> <textarea cols="40" rows="5" name="mtext"><?= $mtext ?></textarea> <br> <input type="submit" name="submit" value="submit">
If you want to provide a default value in the multiline text field (for example, “Enter your data here …”), provide this instead of the empty string in the PHP code:
<?php $mtext = $_POST['mtext'] ?? 'Enter your data here ...'; $mtext = htmlspecialchars($mtext); ?> <form method="post" action="example.php"> Multiline text:<br> <textarea cols="40" rows="5" name="mtext"><?= $mtext ?></textarea> <br> <input type="submit" name="submit" value="submit">
Prefill Forms using Cookies
Another possibility is to prefill form values from cookies. This is quite useful when users enter their data into a form several times. So, when they visit a form on the site a couple of days later, the old data can be retrieved from the cookie.
The following code retrieves a value from the cookie that contains the field data.
Saving and Retrieving Form Data from Cookies
<?php if (isset($_POST['submit'])) { $uname = htmlspecialchars ($_POST['uname'] ?? ''); $email = htmlspecialchars ($_POST['email'] ?? ''); $mtext = htmlspecialchars ($_POST['mtext'] ?? ''); // Save the form data in cookies when form is submitted setcookie('uname', $uname, strtotime ('+30 days')); setcookie('email', $email, strtotime ('+30 days')); setcookie('mtext', $mtext, strtotime ('+30 days')); } else { // Form not submitted // Retrieve data from cookies (if exist) $uname = $_COOKIE['uname'] ?? ''; $email = $_COOKIE['email'] ?? ''; $mtext = $_COOKIE['mtext'] ?? ''; } ?> <form method="post" action="example.php"> <p>Name: <input type="text" name="uname" value="<?=$uname?>"></p> <p>Email: <input type="email" name="email" value="<?=$email?>"></p> <p>Comments:<br> <textarea name="mtext" rows="5" cols="40"><?=$mtext?></textarea></p> <p><input type="submit" name="submit" value="Submit"></p> </form>
Processing Forms in PHP: