[PHP Code > PHP Text and Textarea]
Text and Textarea
This demo shows the use of the HTML Text and Textarea Controls and how to use PHP variables to gather the user's input after a Submit. This also illustrates how to maintain and show what the user selected -- in other words, make the form sticky.
This also shows how to clean the user data received via htmlentities() for output to a Browser.
Please note: I have NOT included HTML labels for input statements. This was done to simplify the code and concept. If you are going to create forms for your websites, then you should also use labels to identify the HTML controls for the disabled. Please review the demos provided -- these show lables for all input controls requiring user action.
You may use the following script as you need without any obligation to me whatsoever. However, a Donation via PayPal would be greatly appreciated -- please note, this site does not accept advertising.
Here's the PHP/HTML code for the "Text and TextArea" Control:
00
07 <?php 08 09 error_reporting(E_ALL); // set error reporting to all 10 $self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME'])); 11 12 $first_name = isset($_POST['first_name']) ? $_POST['first_name'] : null; 13 $last_name = isset($_POST['last_name']) ? $_POST['last_name'] : null; 14 $message = isset($_POST['message']) ? $_POST['message'] : null; 15 16 // clean the data received (i.e., make harmless any text provided) 17 18 $first_name = htmlentities($first_name); 19 $last_name = htmlentities($last_name); 20 $message = htmlentities($message); 21 22 ?> 23 24 24 <!doctype html> 24 <html lang="en-us"> 24 24 <head> 24 <meta charset="utf-8"> 24 <title>tedd's Examples</title> 24 </head> 24 24 <body> 25 45 <form action="<?php echo($self); ?>" method="POST" class="examples"> 46 <table> 47 <tr> 48 <td class="right"> 49 First Name: 50 </td> 51 <td class="left"> 52 <input type="text" size="36" name="first_name" value="<?php echo($first_name);?>"> 53 </td> 54 </tr> 55 <tr> 56 <td class="right"> 57 Last Name: 58 </td> 59 <td class="left"> 60 <input type="text" size="36" name="last_name" value="<?php echo($last_name);?>"> 61 </td> 62 </tr> 63 <tr> 64 <td class="right"> 65 Message: 66 </td> 67 <td class="left"> 68 <textarea name="message" class="message" rows=4 cols=60><?php echo($message);?></textarea> 69 </td> 70 </tr> 71 </table> 72 <p> 73 <input type="submit" name="submit" value="Submit"> 74 </p> 75 </form> 76 76 </body> 76 </html>
Show the above CODE for Copy/Paste below.
The following code will work "as-is" without any additional coding. However, remember to use a .php' suffix.
<?php error_reporting(E_ALL); // set error reporting to all $self = htmlspecialchars(basename($_SERVER['SCRIPT_NAME'])); $first_name = isset($_POST['first_name']) ? $_POST['first_name'] : null; $last_name = isset($_POST['last_name']) ? $_POST['last_name'] : null; $message = isset($_POST['message']) ? $_POST['message'] : null; // clean the data received (i.e., make harmless any text provided) $first_name = htmlentities($first_name); $last_name = htmlentities($last_name); $message = htmlentities($message); ?> <!doctype html> <html lang="en-us"> <head> <meta charset="utf-8"> <title>tedd's Examples</title> </head> <body> <form action="<?php echo($self); ?>" method="POST" class="examples"> <table> <tr> <td class="right"> First Name: </td> <td class="left"> <input type="text" size="36" name="first_name" value="<?php echo($first_name);?>"> </td> </tr> <tr> <td class="right"> Last Name: </td> <td class="left"> <input type="text" size="36" name="last_name" value="<?php echo($last_name);?>"> </td> </tr> <tr> <td class="right"> Message: </td> <td class="left"> <textarea name="message" class="message" rows=4 cols=60><?php echo($message);?></textarea> </td> </tr> </table> <p> <input type="submit" name="submit" value="Submit"> </p> </form> </body> </html>