Andrew De Forest
Andrew De Forest

Reputation: 7338

Issue using $_POST with a textarea

I have a simple contact form on a website that has 2 text fields, 1 textarea, and 1 hidden field.

For some reason, all the fields POST to a PHP script except the textarea. I have done this a thousand times before and never had this issue.

Here is my HTML:

<form action="scripts/contactform.php" method="post">
<table width="0" border="0" cellspacing="3" cellpadding="5" class="gpass">
  <tr>
    <td>Name:</td>
    <td><input name="name" type="text" maxlength="50" /></td>
  </tr>
  <tr>
    <td>E-mail:</td>
    <td><input name="email" type="text"/></td>
  </tr>
  <tr>
    <td>Message:</td>
    <td><textarea name="comment" id="comment" cols="30" rows="5"></textarea>
    <input type="hidden" value=" <?php echo $_SERVER['REMOTE_ADDR'];?>" name="address" />
    </td>
  </tr>
  <tr>
    <td colspan="2" align="center"><input name="submit" type="submit" value="Submit" class="noround" id="regbut" /><input name="reset" type="reset" value="Reset" class="noround" id="regbut"/></td>
  </tr>
</table>
</form>

And my script looks like this:

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) 
{
    die('Failed to connect to server: ' . mysql_error());
}

$db = mysql_select_db(DB_DATABASE);
if(!$db) 
{
    die("Unable to select database");
}

$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$comment = mysql_real_escape_string($_POST['comment']);
$ipaddress = mysql_real_escape_string($_POST['address']);

I have a few things to process the data underneath this, but that doesn't matter since the $comment variable isn't being defined. I've searched the entire script and there are no conflicting variable names.

I am completely stumped on why this is happening. I've successfully processed textarea's on my site multiple times before, so this really is confusing.

Upvotes: 5

Views: 10934

Answers (3)

Tacho
Tacho

Reputation: 11

You only need to add an ID value to the form and then add the form attribute to the textarea with the form ID value

<form id="sample".....>
<textarea name="aba" form="sample".....></textarea>
</form>

Upvotes: 1

SteveCinq
SteveCinq

Reputation: 1963

Though in your case you don't have the textarea set to disabled, the reason I found this post was because I wasn't getting a value from a textarea that was. So here's a note for anyone else with that issue.

To POST the value from a textarea where you want the field to be non-editiable, use readonly instead of disabled - either directly in html or via setAttribute in JavaScript - and then use CSS to highlight it, eg:

textarea[readonly] {background-color:#F0F0F0;})

Upvotes: 1

Pateman
Pateman

Reputation: 2757

I once experienced an error similar to yours. What helped me was to use different id and name parameters. Try and see for yourself, because you have them identical here.

Upvotes: 6

Related Questions