Jump to content

Recommended Posts

Hello everybody. I'm a new php freak from UK. I need help with processing a test form.

The problem I am having is that the php process does not seem to recognise an empty <textarea> name="comments" section. If i leave the textarea blank no error message comes up, yet it does with all the other fields. I hope this is ok for a first post! The code is as follows:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

  <title>Form Proccesing</title>

  <meta http-equiv="content-type" content="text/html;charset=utf-8" />

  <meta http-equiv="Content-Style-Type" content="text/css" />

      <style type="text/css">@import url(form.css);</style>

</head>

<body>

 

<?php

$first_name=$_POST['first_name'];

$second_name=$_POST['second_name'];

$email=$_POST['email'];

$comments=$_POST['comments'];

 

//first name checker

if (!empty ($_POST['first_name']))

{

$first_name=$_POST['first_name'];

}

 

else

{

$first_name=NULL;

echo '<p class="error">You need to enter a first name</p>';

}

 

//second name checker

if (!empty ($_POST['second_name']))

{

$second_name=$_POST['second_name'];

}

 

else

{

$second_name=NULL;

echo '<p class="error">You need to enter a second name</p>';

}

 

//email checker

if (!empty ($_POST['email']))

{

$email=$_POST['email'];

}

 

else

{

$email=NULL;

echo '<p class="error">You need to enter a valid email.</p>';

}

 

//comment checker

if (!empty($_POST['comments']))

{

$comments=$_POST['comments'];

}

 

else

{

$comments=NULL;

echo '<p class="error">You need to enter a comment</p>';

}

 

if ($first_name && $second_name && $email && $comments)

{

echo "<p>Thank you $first_name $second_name and thank you for the following comments: \"$comments\". We will reply at $email</p>";

}

 

 

?>

 

 

</body>

</html>

 

 

The HTML for the form itself is:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

  <title>Form</title>

  <meta http-equiv="content-type" content="text/html;charset=utf-8" />

  <meta http-equiv="Content-Style-Type" content="text/css" />

      <style type="text/css">@import url(form.css);</style>

</head>

 

 

<body>

 

      <form action="form_processor.php" method="post" >

      <fieldset>

<legend>Please fill in your details below</legend>

            <p>First Name:

<input type="text" name="first_name" size="40" maxlength="60" />

</p>

<p>Second Name:

<input type="text" name="second_name" size="40" maxlength="60" />

        </p>

<p>Please select:

<input type="radio" name="gender" value="M" />Male

<input type="radio" name="gender" value="F" />Female

</p>

<p>Please enter your email address:

<input type="text" name="email" size="40" maxlength="60"/>

</p>

<p>Please select your age   

  <select name="age">

        <option value="20-30">20-30</option>

        <option value="40-50">40-50</option>

        <option value="60+">60+</option>

  </select>

</p>

<p>

Choose a password:

<input type="password" name="password" size="40" maxlength="60"/>

</p>

<p>

Please leave a comment:

    <textarea name="comments" rows="5" cols="40">

</textarea>

</p>

<p><input type="submit" value="Go!" /></p>

          </fieldset>

      </form>

 

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/139246-solved-form-processing/
Share on other sites

try using trim() like this

//comment checker
if (!empty($_POST['comments']))
{
$comments=trim($_POST['comments']);
}

else
{
$comments=NULL;
echo '<p class="error">You need to enter a comment</p>';
}

you also might want to consider using it on your other fields it will take out spaces \r \n and tabs that are at the start ir end of a string

so someone might enter a space for a first name you script will allow that with trim it will return an empty string and be disallowed

 

Scott.

 

Hello and thanks for the reply. It didn;t work unfortunately. If no comments are entered the final echo doesn't show but the error message for no text entered still doesn't come up.

 

It's strange because all the other warnings show when fields are blank and the code is identical. This is really puzzling me!  ???  :)

Please use the code tags around your code

[ code ]code here[ /code ]

//without spaces inside the [] looks like this

code here

 

if (trim($_POST['comments']) > "")
{
$comments=trim($_POST['comments']);
}

else
{
$comments=NULL;
echo '<p class="error">You need to enter a comment</p>';
}

Think that'll do what you are looking for

Well, I'm thinking that text inputs treat empties as NULL or EMPTY while texareas treat empties as "" (no text but still NULL or EMPTY,  not the same). Also,

<textarea name="comments" rows="5" cols="40">
            </textarea>

is the same as

<textarea name="comments" rows="5" cols="40">            </textarea>

which is not empty, it has a bunch of spaces. So that is why I also included trim() around the variable which got rid of the extra white space.

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.