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

PHPHTMLWeb Development

Avatar of undefined
Last Comment
jordanrynard
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo