/    Sign up×
Community /Pin to ProfileBookmark

Struggles with form validation

I have looked at some PHP tutorials form validation and always there is an error of one sort or another so I downloaded a test form from [URL=”http://www.w3.org/TR/WCAG20-TECHS/working-examples/SCR32/index.php”]http://www.w3.org/TR/WCAG20-TECHS/working-examples/SCR32/index.php[/URL].

This form worked ok on line but when i run it locally, each input field has code inside similar to this:

[CODE]<br /><b>Notice</b>: Undefined variable: strForename in <b>X:xampphtdocsphptest6index.php</b> on line <b>109</b><br />[/CODE]

I am not sure what the problem could be or how to resolve it.
Could someone please advice me.

I am running xampp 1.8.0 and php 5.4.4

thank you.

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmNov 25.2013 — You are trying to use a variable that has not been created. Show us the code. surrounding line 109
Copy linkTweet thisAlerts:
@williamforrestNov 25.2013 — open the index.php file in a text editor and look for the "$strForename", check that line the variable is not initilized
Copy linkTweet thisAlerts:
@priyankagoundNov 25.2013 — I think you must have misplaced $_SESSION for $strForename. You haven't set $strForename equal to anything or you have to place isset($_POST) for $strForename.

Hope this helps.
Copy linkTweet thisAlerts:
@ilurnauthorNov 25.2013 — This is what I do not understand. The script was downloaded from the w3.org site and I have changed nothing. This is the same when I have copied other code from tutorials. I get the same type of error. They work on their websites but no on my xampp.

Could it be something wrong with the install of xampp? I thought it was good but now I am not sure. Simple php scripts work but when I try to validate, it all goes wrong.

This all of the code: Thank you.

[CODE]
<?php
$iErrorCount = 0;
$bSubmitted = false;
$bRegion = 0;
$strError = "<ul>n";

if ($_POST)
{
$strSuggestion = $_POST["suggestion"];
$strOptional = $_POST["optemail"];
$strRating = $_POST["rating"];
$strJibberish = $_POST["jibberish"];
$strForename = $_POST["forename"];
$strAge = $_POST["age"];
$strEmail = $_POST["email"];

if (strlen($_POST["signup"]) > 0)
{
$bSubmitted = true;

if (strlen($strForename) < 2 || is_numeric($strForename))
{
$iErrorCount++;
$strError .= "<li><a href="#forename">Please enter your forename</a></li>n";
}

if (!is_numeric($strAge))
{
$iErrorCount++;
$strError .= "<li><a href="#age">Please enter your age</a></li>n";
}

if (!preg_match("/^[w-.']{1,}@([da-zA-Z-]{1,}.){1,}[da-zA-Z-]{2,}$/", $strEmail))
{
$iErrorCount++;
$strError .= "<li><a href="#email">Please enter your email address</a></li>n";
}
}
else if (strlen($_POST["submit"]) > 0)
{
$bRegion = 1;
$bSubmitted = true;

if (strlen($strSuggestion) < 2 || is_numeric($strSuggestion))
{
$iErrorCount++;
$strError .= "<li><a href="#suggestion">Enter a suggestion</a></li>n";
}

if (strlen($strOptional) > 0 && !preg_match("/^[w-.']{1,}@([da-zA-Z-]{1,}.){1,}[da-zA-Z-]{2,}$/", $strOptional))
{
$iErrorCount++;
$strError .= "<li><a href="#optemail">Please enter your email address (optional)</a></li>n";
}

if (!is_numeric($strRating))
{
$iErrorCount++;
$strError .= "<li><a href="#rating">Please rate this suggestion</a></li>n";
}
}

$strError .= "</ul>n";

if ($iErrorCount > 0)
$strError = "<h2>$iErrorCount Errors in Submission</h2>n" . $strError;
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Form Validation</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="css/validate.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="scripts/validate.js"></script>
</head>
<body>

<h1>Form Validation</h1>
<p>
The following form is validated before being submitted if scripting is available, otherwise the form is validated on the server. All fields are required, except those marked optional. If errors are found in the submission, the form is cancelled and a list of errors is displayed at the top of the form.
</p>

<?php if ($bSubmitted == true && $iErrorCount == 0) { ?>
<h1 id="focuspoint">Successful Submission</h1>
<p>
When it comes to filling out web forms, you rock!
</p>
<?php
}
else if ($iErrorCount > 0 && $bRegion == 0)
echo $strError;

if ($bSubmitted == false || ($bSubmitted == true && $iErrorCount != 0)) {
?>


<p>
Please enter your details below.
</p>

<h2>Validating Form</h2>
<form id="personalform" method="post" action="index.php">
<div class="validationerrors"></div>
<fieldset>
<legend>Personal Details</legend>
<p>
<label for="forename">Please enter your forename</label>
<input type="text" size="20" name="forename" id="forename" class="string" value="<?php echo $strForename; ?>">
</p>
<p>
<label for="age">Please enter your age</label>
<input type="text" size="20" name="age" id="age" class="number" value="<?php echo $strAge; ?>">
</p>
<p>
<label for="email">Please enter your email address</label>
<input type="text" size="20" name="email" id="email" class="email" value="<?php echo $strEmail; ?>">
</p>
</fieldset>
<p>
<input type="submit" name="signup" value="Sign up">
</p>
</form>
<?php
}
if ($iErrorCount > 0 && $bRegion == 1)
echo $strError;

if ($bSubmitted == false || ($bSubmitted == true && $iErrorCount != 0)) {

?>
<h2>Second Form</h2>
<form id="secondform" method="post" action="index.php#focuspoint">
<div class="validationerrors"></div>
<fieldset>
<legend>Second Form Details</legend>
<p>
<label for="suggestion">Enter a suggestion</label>
<input type="text" size="20" name="suggestion" id="suggestion" class="string" value="<?php echo $strSuggestion; ?>">
</p>
<p>
<label for="optemail">Please enter your email address (optional)</label>
<input type="text" size="20" name="optemail" id="optemail" class="optional email" value="<?php echo $strOptional; ?>">
</p>
<p>
<label for="rating">Please rate this suggestion</label>
<input type="text" size="20" name="rating" id="rating" class="number" value="<?php echo $strRating; ?>">
</p>
<p>
<label for="jibberish">Enter some jibberish (optional)</label>
<input type="text" size="20" name="jibberish" id="jibberish" value="<?php echo $strJibberish; ?>">
</p>
</fieldset>
<p>
<input type="submit" name="submit" value="Add Suggestion">
</p>
</form>
<?php } ?>
</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@ginerjmNov 25.2013 — Try turning on error checking so that you can see if there are any errors in your script?
Copy linkTweet thisAlerts:
@ilurnauthorNov 25.2013 — I checked the code with http://phpcodechecker.com/ and it is ok. Also this is the same with all scripts. I think that is too coincidental to be the same error with all scripts.

This is my set up.

xampp as a local server, Sublime Text is my editor, but the thing is, I am not editing this script. I downloaded it, unzipped it an run it to see if there are differences with other scripts. The errors are the same. I am confused with PHP.

thank you for helping.
Copy linkTweet thisAlerts:
@ginerjmNov 25.2013 — IF you turn on error checking YOU might find out what is wrong with your installation, if that is it.
Copy linkTweet thisAlerts:
@ilurnauthorNov 25.2013 — I have installed an error checker into Sublimetext and that checks for syntax errors. I have also checked it with phpcodechecker and there are not any php problems (that I can find).

I have tried other browsers but the error is there.

Has anyone seen a similar problem?

thank you.
Copy linkTweet thisAlerts:
@ginerjmNov 25.2013 — So - you refuse the help we offer you because you THINK you have done everything.

Good luck. ?
Copy linkTweet thisAlerts:
@ilurnauthorNov 26.2013 — Why do you say that? What help have I refused? I have not refused any help and, no, I do not think I tried everything, that is why I am here.

You asked me to try error checking. I did and did not find any errors. I am only saying what is happening. (or not). If there is another error checker I should use, please tell me. I am new to this and trying hard to find my way.

As for the installation, I do not know if it is the problem or something else. It could be one of many things such as xampp, php, or me, I do not know. Perhaps if you would be kind enough to try the code from the link I posted in the beginning and tell me if you are seeing errors or not then at least I will know if the code is wrong or something else.

thank you. And your help is appreciated.
Copy linkTweet thisAlerts:
@ilurnauthorNov 26.2013 — I am going back over the replies to see if I missed something I was asked to do.

I think I answered #2, #3 and #4 with the code I posted in #5 but will say it again.

williamforrest
open the index.php file in a text editor and look for the "$strForename", check that line the variable is not initilized
[/QUOTE]

These lines have $strForename
[CODE]Line 13 $strForename = $_POST["forename"];
Line 21 if (strlen($strForename) < 2 || is_numeric($strForename))
Line 109 <input type="text" size="20" name="forename" id="forename" class="string" value="<?php echo $strForename; ?>">[/CODE]


How is it initialized. (php is all new to me so I am stumbling)

priyankagound

This is where I do not understand. I downloaded and run it. I did not change anything. I wonder if the download script is different to the one used on the example page?

thank you.
Copy linkTweet thisAlerts:
@arronmattwillsNov 26.2013 — This is what I do not understand. The script was downloaded from the w3.org site and I have changed nothing. This is the same when I have copied other code from tutorials. I get the same type of error. They work on their websites but no on my xampp.

Could it be something wrong with the install of xampp? I thought it was good but now I am not sure. Simple php scripts work but when I try to validate, it all goes wrong.

This all of the code: Thank you.

[CODE]
<?php
$iErrorCount = 0;
$bSubmitted = false;
$bRegion = 0;
$strError = "<ul>n";

if ($_POST)
{
$strSuggestion = $_POST["suggestion"];
$strOptional = $_POST["optemail"];
$strRating = $_POST["rating"];
$strJibberish = $_POST["jibberish"];
$strForename = $_POST["forename"];
$strAge = $_POST["age"];
$strEmail = $_POST["email"];

if (strlen($_POST["signup"]) > 0)
{
$bSubmitted = true;

if (strlen($strForename) < 2 || is_numeric($strForename))
{
$iErrorCount++;
$strError .= "<li><a href="#forename">Please enter your forename</a></li>n";
}

if (!is_numeric($strAge))
{
$iErrorCount++;
$strError .= "<li><a href="#age">Please enter your age</a></li>n";
}

if (!preg_match("/^[w-.']{1,}@([da-zA-Z-]{1,}.){1,}[da-zA-Z-]{2,}$/", $strEmail))
{
$iErrorCount++;
$strError .= "<li><a href="#email">Please enter your email address</a></li>n";
}
}
else if (strlen($_POST["submit"]) > 0)
{
$bRegion = 1;
$bSubmitted = true;

if (strlen($strSuggestion) < 2 || is_numeric($strSuggestion))
{
$iErrorCount++;
$strError .= "<li><a href="#suggestion">Enter a suggestion</a></li>n";
}

if (strlen($strOptional) > 0 && !preg_match("/^[w-.']{1,}@([da-zA-Z-]{1,}.){1,}[da-zA-Z-]{2,}$/", $strOptional))
{
$iErrorCount++;
$strError .= "<li><a href="#optemail">Please enter your email address (optional)</a></li>n";
}

if (!is_numeric($strRating))
{
$iErrorCount++;
$strError .= "<li><a href="#rating">Please rate this suggestion</a></li>n";
}
}

$strError .= "</ul>n";

if ($iErrorCount > 0)
$strError = "<h2>$iErrorCount Errors in Submission</h2>n" . $strError;
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Form Validation</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="css/validate.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="scripts/validate.js"></script>
</head>
<body>

<h1>Form Validation</h1>
<p>
The following form is validated before being submitted if scripting is available, otherwise the form is validated on the server. All fields are required, except those marked optional. If errors are found in the submission, the form is cancelled and a list of errors is displayed at the top of the form.
</p>

<?php if ($bSubmitted == true && $iErrorCount == 0) { ?>
<h1 id="focuspoint">Successful Submission</h1>
<p>
When it comes to filling out web forms, you rock!
</p>
<?php
}
else if ($iErrorCount > 0 && $bRegion == 0)
echo $strError;

if ($bSubmitted == false || ($bSubmitted == true && $iErrorCount != 0)) {
?>


<p>
Please enter your details below.
</p>

<h2>Validating Form</h2>
<form id="personalform" method="post" action="index.php">
<div class="validationerrors"></div>
<fieldset>
<legend>Personal Details</legend>
<p>
<label for="forename">Please enter your forename</label>
<input type="text" size="20" name="forename" id="forename" class="string" value="<?php echo $strForename; ?>">
</p>
<p>
<label for="age">Please enter your age</label>
<input type="text" size="20" name="age" id="age" class="number" value="<?php echo $strAge; ?>">
</p>
<p>
<label for="email">Please enter your email address</label>
<input type="text" size="20" name="email" id="email" class="email" value="<?php echo $strEmail; ?>">
</p>
</fieldset>
<p>
<input type="submit" name="signup" value="Sign up">
</p>
</form>
<?php
}
if ($iErrorCount > 0 && $bRegion == 1)
echo $strError;

if ($bSubmitted == false || ($bSubmitted == true && $iErrorCount != 0)) {

?>
<h2>Second Form</h2>
<form id="secondform" method="post" action="index.php#focuspoint">
<div class="validationerrors"></div>
<fieldset>
<legend>Second Form Details</legend>
<p>
<label for="suggestion">Enter a suggestion</label>
<input type="text" size="20" name="suggestion" id="suggestion" class="string" value="<?php echo $strSuggestion; ?>">
</p>
<p>
<label for="optemail">Please enter your email address (optional)</label>
<input type="text" size="20" name="optemail" id="optemail" class="optional email" value="<?php echo $strOptional; ?>">
</p>
<p>
<label for="rating">Please rate this suggestion</label>
<input type="text" size="20" name="rating" id="rating" class="number" value="<?php echo $strRating; ?>">
</p>
<p>
<label for="jibberish">Enter some jibberish (optional)</label>
<input type="text" size="20" name="jibberish" id="jibberish" value="<?php echo $strJibberish; ?>">
</p>
</fieldset>
<p>
<input type="submit" name="submit" value="Add Suggestion">
</p>
</form>
<?php } ?>
</body>
</html>
[/CODE]
[/QUOTE]


why don't you write your own(custom code) ?
Copy linkTweet thisAlerts:
@ilurnauthorNov 26.2013 — why don't you write your own(custom code) ?[/QUOTE]

I wish. I am only starting to learn PHP. I was following tutorials online to learn and getting the same error from all tutorials so I thought perhaps I am doing something wrong so I download this zip file from W3.org. I still have the same error even although I did not touch this code.

This is why I think there is perhaps an error in my setup and was asking if others have seen a similar problem. I have already installed xampp two times - which installs PHP. Is the install of wamp different to xampp, do you know?

thank you
Copy linkTweet thisAlerts:
@stradmadhuDec 02.2013 — Try this:


<?php

$iErrorCount = 0;

$bSubmitted = false;

$bRegion = 0;

$strError = "<ul>n";

if ($_POST)
{
$strSuggestion = $_POST["suggestion"];
$strOptional = $_POST["optemail"];
$strRating = $_POST["rating"];
$strJibberish = $_POST["jibberish"];
$strForename = $_POST["forename"];
$strAge = $_POST["age"];
$strEmail = $_POST["email"];

if (strlen($_POST["signup"]) > 0)
{
$bSubmitted = true;

if (strlen($strForename) < 2 || is_numeric($strForename))
{
$iErrorCount++;
$strError .= "<li><a href="#forename">Please enter your forename</a></li>n";
}

if (!is_numeric($strAge))
{
$iErrorCount++;
$strError .= "<li><a href="#age">Please enter your age</a></li>n";
}

if (!preg_match("/^[w-.']{1,}@([da-zA-Z-]{1,}.){1,}[da-zA-Z-]{2,}$/", $strEmail))
{
$iErrorCount++;
$strError .= "<li><a href="#email">Please enter your email address</a></li>n";
}
}
else if (strlen($_POST["submit"]) > 0)
{
$bRegion = 1;
$bSubmitted = true;

if (strlen($strSuggestion) < 2 || is_numeric($strSuggestion))
{
$iErrorCount++;
$strError .= "<li><a href="#suggestion">Enter a suggestion</a></li>n";
}

if (strlen($strOptional) > 0 && !preg_match("/^[w-.']{1,}@([da-zA-Z-]{1,}.){1,}[da-zA-Z-]{2,}$/", $strOptional))
{
$iErrorCount++;
$strError .= "<li><a href="#optemail">Please enter your email address (optional)</a></li>n";
}

if (!is_numeric($strRating))
{
$iErrorCount++;
$strError .= "<li><a href="#rating">Please rate this suggestion</a></li>n";
}
}

$strError .= "</ul>n";

if ($iErrorCount > 0)
$strError = "<h2>$iErrorCount Errors in Submission</h2>n" . $strError;
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>

<title>Form Validation</title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<link href="css/validate.css" rel="stylesheet" type="text/css">

<script type="text/javascript" src="scripts/validate.js"></script>

</head>

<body>

<h1>Form Validation</h1>

<p>

The following form is validated before being submitted if scripting is available, otherwise the form is validated on the server. All fields are required, except those marked optional. If errors are found in the submission, the form is cancelled and a list of errors is displayed at the top of the form.

</p>

<?php if ($bSubmitted == true && $iErrorCount == 0) { ?>

<h1 id="focuspoint">Successful Submission</h1>

<p>

When it comes to filling out web forms, you rock!

</p>

<?php

}

else if ($iErrorCount > 0 && $bRegion == 0)

echo $strError;

if ($bSubmitted == false || ($bSubmitted == true && $iErrorCount != 0)) {
?>


<p>

Please enter your details below.

</p>

<h2>Validating Form</h2>

<form id="personalform" method="post" action="vali_1.php">

<div class="validationerrors"></div>

<fieldset>

<legend>Personal Details</legend>

<p>

<label for="forename">Please enter your forename</label>

<input type="text" size="20" name="forename" id="forename" class="string" value="<?php echo $strForename; ?>">

</p>

<p>

<label for="age">Please enter your age</label>

<input type="text" size="20" name="age" id="age" class="number" value="<?php echo $strAge; ?>">

</p>

<p>

<label for="email">Please enter your email address</label>

<input type="text" size="20" name="email" id="email" class="email" value="<?php echo $strEmail; ?>">

</p>

</fieldset>

<p>

<input type="submit" name="signup" value="Sign up">

</p>

</form>

<?php

}

if ($iErrorCount > 0 && $bRegion == 1)

echo $strError;

if ($bSubmitted == false || ($bSubmitted == true && $iErrorCount != 0)) {

?>

<h2>Second Form</h2>

<form id="secondform" method="post" action="vali_1.php">

<div class="validationerrors"></div>

<fieldset>

<legend>Second Form Details</legend>

<p>

<label for="suggestion">Enter a suggestion</label>

<input type="text" size="20" name="suggestion" id="suggestion" class="string" value="<?php echo $strSuggestion; ?>">

</p>

<p>

<label for="optemail">Please enter your email address (optional)</label>

<input type="text" size="20" name="optemail" id="optemail" class="optional email" value="<?php echo $strOptional; ?>">

</p>

<p>

<label for="rating">Please rate this suggestion</label>

<input type="text" size="20" name="rating" id="rating" class="number" value="<?php echo $strRating; ?>">

</p>

<p>

<label for="jibberish">Enter some jibberish (optional)</label>

<input type="text" size="20" name="jibberish" id="jibberish" value="<?php echo $strJibberish; ?>">

</p>

</fieldset>

<p>

<input type="submit" name="submit" value="Add Suggestion">

</p>

</form>

<?php } ?>

</body>

</html>



hope this code helps:
Copy linkTweet thisAlerts:
@wevieDec 03.2013 — I think that the problem is that you are trying to echo undefined variables as the values in your input boxes and this is why you are getting the errors. The reason these are echoed this way is because without them, if someone enters data, and your code does not like what is entered and displays an error message, all the inputs will go away.

A common way to prevent this error is to set all of those variables to empty in the beginning. Try putting the below code at the top of the file with the rest of your variables and see what that does. I think it will fix your problem, although I could be wrong.

[code=php]
$strForename = $strAge = $strEmail = '';
[/code]
Copy linkTweet thisAlerts:
@unasAquilaDec 04.2013 — Just a cursory glance and I notice your first issue is that your setting variables with unknown or empty values which are undefined
[code=php]
$strSuggestion = $_POST["suggestion"];
[/code]

try doing this
[code=php]
$strSuggestion = isset($_POST["suggestion"]) ? $_POST["suggestion"] : null;
[/code]

then just check if the variables are null before doing other checks
[code=php]
if(is_null($strSuggestion)){
$iErrorCount++;
$strError .= "<li>Your error message</li>n";
}
[/code]


this should resolve your undefined issues.
×

Success!

Help @ilurn 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.18,
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,
)...