Link to home
Start Free TrialLog in
Avatar of ResourcefulDB
ResourcefulDB

asked on

php embed in html bug

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.

Thanks,
RDB

 
<?php
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";
 else
 {

 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>
</form>

Open in new window

Avatar of jordanrynard
jordanrynard

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

ASKER CERTIFIED SOLUTION
Avatar of jordanrynard
jordanrynard

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial