php embed in html bug

ResourcefulDB used Ask the Experts™
Hi all,

I am pretty new to php. Here is one simple program I try to work out how php embed in html. It gives one funny result. In one occasion, I fill in only name and email, but not the message. The first time I click on submit button, it tell me the length of message is 0. Then I click on it one more time, it tell me the length is 2. Every time, we click the button, the lenght increase by 2.

Please help me to understand why it does that.


if (isset($_POST['name']))
 $name = $_POST['name'];  
 $email = $_POST['email'];
 $message = $_POST['message'];
 $errorstring = "";
 if (!$name)
 $errorstring = $errorstring."Name<br>";
 if (!$email)
 $errorstring = $errorstring."Email<br>";
 if (!$message)
 $errorstring = $errorstring."Message<br>";
 echo "The length of message is ".strlen($message);
 echo "<br>";

 if ($errorstring!="")
 echo "Please fill out the following fields:<br> $errorstring";

 echo "Success!";
 echo "<form action='formvalidation.php' method='POST'>";
 echo "</form>";



<form action="formvalidation.php" method='POST'>
Name: <input type='text' name='name' value='<?php echo $name; ?>'> <br>
Email: <input type='text' name='email' > <br>
Message: <textarea name='message' rows='5' cols='40'> <?php echo $message; ?> </textarea> <br>
<input type="submit" name="click" value='Send'> <br>

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
This is because the value of your textarea is not empty...
note the spaces that exists beside your echo:
<textarea name='message' rows='5' cols='40'> <?php echo $message; ?> </textarea>

Open in new window

Those spaces are actually being produced in HTML, each with a string length of 1.

Try removing them like so:
<textarea name='message' rows='5' cols='40'><?php echo $message; ?></textarea>

Open in new window

So each time you submit the form, you're combining the previous submission of message, (which starts with 2 spaces), and adds another 2 spaces to the string $message, every time you submit it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial