www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Struggles with form validation

  1. #1
    Join Date
    Sep 2009
    Posts
    25

    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 http://www.w3.org/TR/WCAG20-TECHS/wo...CR32/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:\xampp\htdocs\phptest6\index.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.

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    731
    You are trying to use a variable that has not been created. Show us the code. surrounding line 109

  3. #3
    Join Date
    Oct 2013
    Posts
    30
    open the index.php file in a text editor and look for the "$strForename", check that line the variable is not initilized

  4. #4
    Join Date
    Sep 2013
    Posts
    221
    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.
    strad solutionswww.stradsolutions.com

  5. #5
    Join Date
    Sep 2009
    Posts
    25
    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>

  6. #6
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    731
    Try turning on error checking so that you can see if there are any errors in your script?

  7. #7
    Join Date
    Sep 2009
    Posts
    25
    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.

  8. #8
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    731
    IF you turn on error checking YOU might find out what is wrong with your installation, if that is it.

  9. #9
    Join Date
    Sep 2009
    Posts
    25
    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.

  10. #10
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    731
    So - you refuse the help we offer you because you THINK you have done everything.

    Good luck.

  11. #11
    Join Date
    Sep 2009
    Posts
    25
    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.

  12. #12
    Join Date
    Sep 2009
    Posts
    25
    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
    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; ?>">
    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.

  13. #13
    Join Date
    Nov 2013
    Location
    new york
    Posts
    66
    Quote Originally Posted by ilurn View Post
    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>
    why don't you write your own(custom code) ?

  14. #14
    Join Date
    Sep 2009
    Posts
    25
    Quote Originally Posted by arronmattwills View Post
    why don't you write your own(custom code) ?
    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

  15. #15
    Join Date
    Nov 2013
    Posts
    3
    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:

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles