Hello,
I am new to PHP so bare with me please.
I am following this tutorial to create a contact form, [url]http://myphpform.com/
and on the validating forms with PHP section. When I try to run the form with the htmlspecialchars function I am given a “Notice: Undefined Index: [I]variablename
I did find a method of switching off the notices but think it would be better to find a way of handling them instead.
Thanks in advance for any assistance.
HTML page:
<html>
<body>
<form action=”myform2.php” method=”post”>
<p>Your Name: <input type=”text” name=”yourname” /><br />
E-mail: <input type=”text”” name=”email” /></p>
<p>Do you like this website?
<input type=”radio” name=”likeit” value=”Yes” checked=”checked” /> Yes
<input type=”radio” name=”likeit” value=”No” /> No
<input type=”radio” name=”likeit” value=”Not Sure” /> Not sure</p>
<p>Your comments:<br />
<textarea name=”comments” rows=”10″ cols=”40″></textarea></p>
<p><input type=”submit” value=”Send It!”></p>
</form>
</body>
</html>
PHP page:
<?php
$yourname = htmlspecialchars($_POST[‘yourname’]);
$email = htmlspecialchars($
$likeit = htmlspecialchars($_POST[‘likeit’]);
$comments = htmlspecialchars($
/* Validation. The very least you should do is pass all variables through PHP’s htmlspecialchars() function. This function will replace HTML chars like < and > to their HTML version < and >. This prevents possible attackers from exploiting our code by injecting HTML or Javascript code
?>
<html>
<body>
Your name is: <?php echo $_POST[‘yourname’]; ?><br />
Your email is: <?php echo $
<br />
Do you like this website? <?php echo $_POST[‘likeit’]; ?><br />
<br />
Comments:<br />
<?php echo $
</body>
</html>