'Textarea content 'error handling' error when using multiple lines
I've made a form in html/php in which there is a 'textarea' as well. I'm currently implementing error handling; when a mistake has been made in the form, keep the content that has been assigned already by the user.
When a mistake has been made by the user (for example, invalid e-mail address), the message that the user already had typed into the 'textarea', remains there.
However, this is ONLY when a message has been typed in which NO new lines have been used (so by pressing ENTER in the textarea). So in a multiple-line text area, for some reason it doesn't work.
Any ideas on how to fix this?
Part of the code:
if (isset($_GET['textmessage'])) {
$textmessage= $_GET['textmessage'];
echo'<div class="user-details">
<div class="input-box">
<span class="details">Extra message</span>
<textarea style="height: 100px; padding-top: 10px" type="text" name="textmessage" placeholder="message">';
echo $textmessage;
echo '</textarea>
</div>
</div>';}
Part of the action.php:
$textmessage= nl2br(htmlspecialchars($_POST['tekstbericht']));
if (!filter_var($mailFrom, FILTER_VALIDATE_EMAIL)) {
header("Location: form.php?form=invalidemail&name=$name&textmessage=$textmessage");
exit();
}
Solution 1:[1]
So the problem has been solved. It turns out a textarea component has a hidden markup when using multiple lines. An URL can't read multiple lines, as it is a single line itself (hence the error). So the multiple lines have to be converted to a single line string. You'll have to replace both the '\r' and '\n'. In this case the code goes like this:
$textmessage_br = str_replace(array("\r","\n"),"QTEXTQ",$textmessage)
The 'QTEXTQ' can be anything you want, as long as it is some kind of text that won't get typed by the user. When wanting to convert the string back to multiple lines, use the same component, but reversed.
$textmessage= str_replace("QTEXTQQTEXTQ", ("\n"),$_GET['textmessage']);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jeroen van den Heuvel |
