0

I'm hoping there is a simple solution to this. I am using a form to submit some information. One of the boxes is a textarea. When i submit the form it runs this line of code:

if (!isset($_POST['textarea'])

it seems to register that there is type there regardless of whether I actually enter information. Is this a simple fix, or do I need to post code? The form code is as follows:

<form id="form1" enctype="multipart/form-data" name="form1" method="post" action="action.php">
    <table width="618" border="0" cellpadding="10" cellspacing="0">
      <tr>
        <td><textarea name="reason" id="reason" cols="45" rows="10"></textarea></td>
      </tr>
      <tr>
        <td><input type="submit" name="submit" id="submit" value="delete" /></td>
      </tr>
      </table>
  </form>
3
Contributors
4
Replies
8
Views
6 Years
Discussion Span
Last Post by ddymacek
-1
//textarea will always be set when looking at it this way. so 
	if(isset($_POST['textarea'] && $_POST['textarea'] != '') {
		// You have textarea data do something with it...
		$text = $_POST['textarea']
    } else {
		echo "textarea has no value...";
	}
0

It looks like you've not yet been informed about one of the basic principles of working with forms in PHP. I see that you're using $_POST, but in the HTML form you're using below clearly defines the 'name' attribute of your textarea element as 'reason'. It is the 'name' of form elements that you should be using to get the values of those fields from the $_POST array. So in this case you should use $_POST.

Also, something that may help you moving forward: I find that checking if a form filed is not empty more closely meets my needs 9 times out of ten, while isset() tends to only see that a certain key in an array is set, rather than whether it's empty or not. If a user submits a form without bothering to fill out the field, then $_POST will be set, but it will still be empty. So in this case, it seems that checking to make sure that a field is not empty more closely matches your needs as well.

So here is what you probably want to change your above code to look like instead:

//The '!' means not, or opposite. So '!empty($someVar)' means "Not empty"
if( !empty($_POST['reason']) ){
    //execute the code for when the form has properly been filled out
}else{
    //display the form
}

Good luck!

0
// oops... should be I forgot the ) on the isset part...
if(isset($_POST['textarea']) && $_POST['textarea'] != '') {
0

I didn't look at your sample form but admiral is right. you need to look for the field name 'reason', not 'textarea'.

if(isset($_POST['reason'] && $_POST['reason'] != '') {
    // You have textarea data do something with it...
    $text = $_POST['reason']
} else {
    echo "textarea has no value...";
    $text = '';
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.