无法从 PHP 中的 textarea 获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15069567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get value from textarea in PHP
提问by Shai
I'm having issues getting the value of text area in PHP (getting undefined).
我在获取 PHP 中文本区域的值时遇到问题(未定义)。
My code is pretty simple. My HTML is:
我的代码很简单。我的 HTML 是:
<form name="contactform" method="POST" action="submit_form.php">
<input type="text" class="formInput" placeholder="Your Name" id="first_name" maxlength="50" size="20" />
<input type="text" class="formInput" placeholder="Email Address" id="email" maxlength="80" size="20" />
<textarea class="formInput" placeholder="Your Message" name="comments" id="comments" maxlength="1400" cols="20" rows="6"></textarea>
<input type="submit" id="SubmitContactForm" class="myButton" value="Submit" />
</form>
My PHP:
我的PHP:
echo $_POST['comments'];
This results in undefined, no matter what I write inside the textarea. I searched here for some solutions and found some stuff, like adding "name" and "id" and making them both different, adding htmlspecialchars($_POST['comments'])and so on, all of these solutions don't work. I will make a note and say that I can get the other fields values without any problems, just this textarea.
undefined无论我在 textarea 中写什么,这都会导致。我在这里搜索了一些解决方案并找到了一些东西,例如添加“名称”和“ID”并使它们都不同,添加htmlspecialchars($_POST['comments'])等等,所有这些解决方案都不起作用。我会记下并说我可以毫无问题地获取其他字段值,仅此文本区域。
Any ideas what can be the problem?
任何想法可能是什么问题?
回答by oktopus
You haven't defined the name-attribute for your textarea.
Add: name="comments"to it:
您尚未为 textarea 定义名称属性。添加:name="comments"到它:
<textarea class="formInput" name="comments" placeholder="Your Message" id="comments" maxlength="1400" cols="20" rows="6"></textarea>
If it still doesn't show up, make sure that you send your form using method="post"
如果它仍然没有出现,请确保您使用发送表单 method="post"
回答by Derek
You need a name attribute in your textarea name="comments"
您需要在 textarea name="comments" 中有一个 name 属性
<textarea class="formInput" placeholder="Your Message" id="comments" name="comments" maxlength="1400" cols="20" rows="6"></textarea>
回答by stakolee
Try adding this to the script that receivesthe form post:
尝试将其添加到接收表单帖子的脚本中:
print "POST: " . print_r($_POST, true) . "\n"
. "GET: " . print_r($_GET, true) . "\n";
If "comments" show up under "GET" then you haven't set the form's method=post
如果“评论”出现在“GET”下,那么您还没有设置表单的 method=post
回答by c4pone
You forgot to add the name attribute. Your html should look like this
您忘记添加 name 属性。你的 html 应该是这样的
<textarea name="comments" class="formInput" placeholder="Your Message" id="comments" maxlength="1400" cols="20" rows="6"></textarea>
回答by Deepu
<textarea class="formInput" placeholder="Your Message" id="comments" maxlength="1400" cols="20" rows="6" name="yourtextarea"></textarea>
回答by Deepu
First define it like $comment=$_POST['comments'];after that you can do what you want,
Note:Don't use it directly like $_POST['comments'];because in some cases it will thrown some error's/notices etc..
首先定义它$comment=$_POST['comments'];,然后你可以做你想做的事,
注意:不要直接使用它,$_POST['comments'];因为在某些情况下它会抛出一些错误/通知等。
回答by Sidharth Mehra
You could try specifying the form attribute of textarea. In this you set the id of your form. Added advantage is that this allows you to place your textarea outside the form as well. Source: http://www.w3schools.com/tags/tag_textarea.asp
您可以尝试指定 textarea 的 form 属性。在此您设置表单的 id。额外的好处是,这也允许您将 textarea 放置在表单之外。来源:http: //www.w3schools.com/tags/tag_textarea.asp
回答by ZenithS
Remove some of your textareaclass like
删除您的一些textarea课程,例如
<textarea name="Address" rows="3" class="input-text full-width" placeholder="Your Address" ></textarea>
to
到
<textarea name="Address" rows="3" class="full-width" placeholder="Your Address" ></textarea>
It depends on your template (Purchased Template).
Develop including some of JavaScript to get value from correct object on UI
but for elements like input-textjust find $('input[type=text]').
这取决于您的模板(采购模板)。开发包括一些 JavaScript 以从 UI 上的正确对象获取价值,但对于像input-textfind这样的元素$('input[type=text]')。

