I have a simple form with a textarea
field inside it and when posting, the textarea
is not posted to the target page. I have tried many solutions on the web but none of them worked. Any idea how to fix it ?
HTML:
<form action="new_page.php" method="post" id="userform">
<p>Content:<br />
<textarea name="content" rows="15" cols="80" form="usrform">Enter your text</textarea>
</p>
<input type="submit" name="submit" value="Create page"/>
</form>
PHP in new_page.php:
$content = htmlspecialchars($_POST["content"]);
echo $content;
Remove form="usrform"
from your textarea and your code will work.
[or]
Renaming usrform
on your textarea to userform
will also work.
Quick fix
Remove the form="usrform"
from the textarea
OR,
change it to form="userform"
to match the ID you have set on your form
So, what is the form attribute?
The form
attribute (introduced in HTML5) allows you to specify exactly which forms the current element should be related to. You can add multiple values by separating them with a space, indicating the element belongs to multiple forms.
This allows you to have form elements outside of the form itself. If you assign the forms ID to the form attribute, it will be submitted in the forms data.
NB This is not supported in IE
More information: http://www.w3schools.com/tags/att_textarea_form.asp
Try this:
Just remove form="usrform" from textarea && your code will work.
<form action="new_page.php" method="post" id="userform">
<p>Content:<br />
<textarea name="content" rows="15" cols="80">Enter your text</textarea>
</p>
<input type="submit" name="submit" value="Create page"/>
</form>
You can get value from textarea like this.
<?php
$content = htmlspecialchars($_POST["content"]);
echo $content;
?>
Thanks!