/    Sign up×
Community /Pin to ProfileBookmark

PHP validation Problem

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/[/url]
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]” for each part of the information posted into the php form. See my code below.

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($_
POST[’email’]);
$likeit = htmlspecialchars($_POST[‘likeit’]);
$comments = htmlspecialchars($_
POST[‘comments’]);
/* 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 &lt; and &gt;. 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 $_
POST[’email’]; ?><br />
<br />

Do you like this website? <?php echo $_POST[‘likeit’]; ?><br />
<br />
Comments:<br />
<?php echo $_
POST[‘comments’]; ?>
</body>
</html>

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bionoidJan 29.2012 — There was a typo in the html with the form:

[B]index.php[/B]
[CODE]<html>
<body>
<form action="myform2.php" method="post">
<p>Your Name: <input type="text" name="yourname" /><br />
E-mail: <input type=[COLOR="Red"]"text"[/COLOR] 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>[/CODE]


It's better to verify that the data you're extracting was actually posted, also you weren't using the variables with the html protected data:

[B]myform2.php[/B]
[CODE]<?php
[COLOR="red"]$yourname = htmlspecialchars(isset($_POST['yourname']) ? $_POST['yourname'] : '');
$email = htmlspecialchars(isset($_POST['email']) ? $_POST['email'] : '');
$likeit = htmlspecialchars(isset($_POST['likeit']) ? $_POST['likeit'] : '');
$comments = htmlspecialchars(isset($_POST['comments']) ? $_POST['comments'] : '');[/COLOR]
/* 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 &lt; and &gt;. This prevents possible attackers from exploiting our code by injecting HTML or Javascript code */
?>

<html>
<body>
Your name is: <?php echo [COLOR="red"]$yourname[/COLOR]; ?><br />
Your email is: <?php echo [COLOR="red"]$email[/COLOR]; ?><br />
<br />

Do you like this website? <?php echo [COLOR="red"]$likeit[/COLOR]; ?><br />
<br />
Comments:<br />
<?php echo [COLOR="red"]$comments[/COLOR]; ?>
</body>
</html>[/CODE]


Hope that helps.
×

Success!

Help @Alano spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...