Search code examples
phptextarearefreshpage-refresh

Retain textarea information if page accidentally refreshes or capcha doesn't work


I have managed to find answers on the input areas for text and have that working. I have a contact form, two inputs - name & email - and a textarea for information.

Looking online I find answers to use this line of code in the value to retain the information if there is a page refresh -

<input type="text" name="name" placeholder="Your Name" value='<?php echo 
isset($_POST['name']) ? $_POST['name'] : ''; ?>' required>

Although when I add the value='' to my text area it doesn't work like the others. I changed the names to match too.

Here is the physical page so you can see for yourself - idwithin.xyz/contact.php and I'll paste all my code below.

Thank you.

<div class="contactForm">
<form method="post" action="">
<input type="text" name="name" placeholder="Your Name" value='<?php echo 
isset($_POST['name']) ? $_POST['name'] : ''; ?>' required>

<input type="text" name="email" placeholder="Your Email" value='<?php echo 
isset($_POST['email']) ? $_POST['email'] : ''; ?>' required>

<textarea type="text" name="message" placeholder="Your Message" value='<? 

php echo isset($_POST['message']) ? $_POST['message'] : ''; ?>' required>


Solution

  • You don't add value="" to textareas, you render the value between the tags:

    <textarea><?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message']) : ''; ?></textarea>