Shai
Shai

Reputation: 1163

Can't get value from textarea in PHP

I'm having issues getting the value of text area in PHP (getting undefined).

My code is pretty simple. My HTML is:

<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:

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.

Any ideas what can be the problem?

Upvotes: 0

Views: 24448

Answers (8)

Sidharth Mehra
Sidharth Mehra

Reputation: 1

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

Upvotes: 0

user3931708
user3931708

Reputation:

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..

Upvotes: 0

ZenithS
ZenithS

Reputation: 1007

Remove some of your textarea class like

<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-text just find $('input[type=text]').

Upvotes: -1

stakolee
stakolee

Reputation: 960

Try adding this to the script that receives the 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

Upvotes: 0

Derek
Derek

Reputation: 2735

You need a name attribute in your textarea name="comments"

<textarea class="formInput" placeholder="Your Message" id="comments" name="comments" maxlength="1400" cols="20" rows="6"></textarea>

Upvotes: 1

웃웃웃웃웃
웃웃웃웃웃

Reputation: 11984

<textarea class="formInput" placeholder="Your Message" id="comments" maxlength="1400" cols="20" rows="6" name="yourtextarea"></textarea>

Upvotes: 0

c4pone
c4pone

Reputation: 797

You forgot to add the name attribute. Your html should look like this

<textarea name="comments" class="formInput" placeholder="Your Message" id="comments" maxlength="1400" cols="20" rows="6"></textarea>

Upvotes: 0

oktopus
oktopus

Reputation: 1767

You haven't defined the name-attribute for your textarea. Add: name="comments" to it:

<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"

Upvotes: 1

Related Questions