Jump to content

How do you check if textarea is empty?


drayarms

Recommended Posts

Hi, I have a form containg three text inputs and one text area.  On the php page that processes the form, I want to check first to make sure the user entered something (input field is not empty) before processing the form. I'll just provide a skeleton of form processing page below. *(I'm save space and time by omitting the form itself but he text inputs fields have the following name attributes "name", "company", "email",  and the textarea field's name attribute is  "message" )  I'll do my best to explain the code in further detail within the code comments.

 

 





<?php




//Turn on output buffering. Allows for headers to be called anywhere on script. See pg228 Ulman.
ob_start();


if (isset($_POST['submit'])) {

// Initialize a session to keep tract of error and success messages:
session_start();


//Define a session variable that keeps tract of how many times user accesses page.
$_SESSION['views'] = 1;


// Connect to the database.

        require('config/config.php');


//Check for errors.


//Check to make sure they entered their name.

if (!empty ( $_POST['name']))  {

        $a = TRUE;

    } else {

        $a = FALSE;

        //This variable will be echoed on the form if field is missing a value
        $_SESSION['name'] = '*Please enter a valid name.'; 


   }



//Check to make sure they entered their company name.

if (!empty ( $_POST['company'])) {

        $b = TRUE;

    } else {

        $b = FALSE;

        //This variable will be echoed on the form if field is missing a value
        $_SESSION['company'] = '*Please enter the name of an institution you are affiliated with.';  


   }



//Check to make sure email is valid.

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email'])) { 


         $c = TRUE; 


}else { 

         $c = FALSE;

      //This variable will be echoed on the form if field is missing a value or email format is wrong.
      $_SESSION['email'] = '*Please enter a valid email address.'; 

      } 




//Check to make sure they entered their message.

if (!empty ( $_POST['message'])) {

        $d = TRUE;

    } else {

        $d = FALSE;

       //This variable will be echoed on the form if field is missing a value
        $_SESSION['message']  = '*Please enter your message.';     


   }






//If no errors

if (empty($_SESSION['name'] ) && empty($_SESSION['company'] ) && empty($_SESSION['email'] ) && empty($_SESSION['message'] ) ) {


//Insert data into database.

        $query = "INSERT INTO table...     ;
       
        $result = mysql_query($query) or die("Data could not be inserted into table because: " .mysql_error());



if (mysql_affected_rows() == 1) {

    
        	// Display success message

                //This variable will be echoed on the form if everything is filled out correctly.
       	 	$_SESSION['sent'] =     "Your email has been sent.  We will get back to you shortly.";


        	// Display contact page (the page containing the form)

	header("Location: contact_page.php");

        exit();


}else{die(mysql_error());}
            


//Display error messages.


} else {// if errors array is not empty


        	// Display page (page containing the form)

	header("Location: contact_page.php"); 

	exit();


        
    }//End of if there are errors.


}//End of if submit.




?>


 

 

 

 

 

So there it is. Everything works fine except that the part which checks if the textarea (named "message") is empty, doesn't work.  If I click the submit button without filling out any field, the appropriate error messages are printed for the text input fields but not the text area field.  If I fill out all fields correctly and leave the "message" field blank, the form gets processed correctly and values are placed in the database and the success message is printed out.  This is also the case when I enter a value for "message" textarea field.  Now I tried something.  I changed the textarea to a text field and the $_SESSION['message'] variable finally got echoed when I left this field blank.  So apparently, the problem arises from the fact that the named attribute from a textarea tag is not being processed correctly??  Who knows what's going wrong here?

Link to comment
Share on other sites

Can't you use isset to check.

 

if (isset( $_POST['name']))  {

        $a = TRUE;  
       $msg[] = "you entered your name";

    } else {

        $a = FALSE;
        $error[] ="you failed to enter a name";
     }

 

Then display the errors

 

if(@$error)
   {
    
    $error[] = "";
    $errors = array($error);
    foreach ($error as $v) 
     {
      echo "$v\n";
     }           
   }

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.