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
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 />
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.
[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]
open the index.php file in a text editor and look for the "$strForename", check that line the variable is not initilized
[/QUOTE]
[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]
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]
[/QUOTE]
<?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]
why don't you write your own(custom code) ?[/QUOTE]
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;
}
if ($bSubmitted == false || ($bSubmitted == true && $iErrorCount != 0)) {
if ($bSubmitted == false || ($bSubmitted == true && $iErrorCount != 0)) {
[code=php]
$strForename = $strAge = $strEmail = '';
[/code]
[code=php]
$strSuggestion = $_POST["suggestion"];
[/code]
[code=php]
$strSuggestion = isset($_POST["suggestion"]) ? $_POST["suggestion"] : null;
[/code]
[code=php]
if(is_null($strSuggestion)){
$iErrorCount++;
$strError .= "<li>Your error message</li>n";
}
[/code]
0.1.9 — BETA 5.18