Jump to content

Recommended Posts

I have created (modified a tutorial) a PHP Validation Form with validation. I am getting a blank page and cannot view it.

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1); ?>
<?php

function VerifyForm(&$values, &$errors)
{
    // Do all necessary form verification

// Validate Model Number
    if (strlen($values['model']) < 4)
        $errors['model'] = 'Model Number too short';
    elseif (strlen($values['model']) > 50)
        $errors['model'] = 'Model Number too long';

// Validate Price
if (is_numeric($values['price']) == 0)
        $errors['price'] = 'No Price has been entered';

// Validate Product
if(($values['product'])=='please_select')
	$errors['product'] = 'No Product has been selected';

// Validate Image
if ((($_FILES["photo"]["type"] != "image/gif")
	|| ($_FILES["photo"]["type"] != "image/jpeg")
	|| ($_FILES["photo"]["type"] != "image/pjpeg"))
	&& ($_FILES["photo"]["size"] > 2000000))
	$errors['photo'] = 'Image format must be either JPG/JPEG/GIF or PNG';

// Validate Description
if (strlen($values['description']) < 10)
        $errors['description'] = 'Description is too short';
        
    return (count($errors) == 0);
}

function DisplayForm($values, $errors)
{
    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>swiftelectrical.net - Test Area</title>
<link rel="stylesheet" href="test.css" type="text/css" media="screen" />
<style>
            TD.error
            {
                color: red;
                font-weight: bold;    
            }
    </style>
</head>
<body>

<div id="container">

	<h1>Add a Product</h1>

<?php
    if (count($errors) > 0)
        echo "<p>There were some errors in your submitted form, please correct them and try again.</p>";
    ?>

	<form method="post" id="customForm" action="<?php = $_SERVER['PHP_SELF'] ?>" enctype="multipart/form-data">

<table>

        <tr>
            <td>Model Number:</td>
            <td><input id="model" type="text" size="30" name="model" value="<?php = htmlentities($values['model']) ?>"/></td>
            <td class="error"><?php = $errors['model'] ?></td>
        </tr>

	<tr>
		<td>Choose a Product:</td>
		<td>
			<select class="product" name="product">
				<option value="please_select">Please select an option below</option>
					<option value="1">19" LCD TV</option>
					<option value="2">22" LCD TV</option>
					<option value="3">26" LCD TV</option>
					<option value="4">32" LCD TV</option>
					<option value="5">37" LCD TV</option>
					<option value="6">42" LCD TV</option>
					<option value="7">37" Plasma TV</option>
					<option value="8">42" Plasma TV</option>
					<option value="9">46" Plasma TV</option>
					<option value="10">50" Plasma TV</option>
					<option value="11">54" Plasma TV</option>
					<option value="12">58" Plasma TV</option>
					<option value="13">Wall Bracket</option>
					<option value="14">Home Cinema System</option>
					<option value="15">Bluray Home Cinema System</option>
					<option value="16">DVD Recorder</option>
					<option value="17">DVD Player</option>
					<option value="18">DVD Portable</option>
					<option value="">Bluray Recorder</option>
					<option value="">Bluray Player</option>
					<option value="">Bluray Portable</option>
					<option value="">Projector</option>
					<option value="">37" LCD TV</option>
					<option value="">42" LCD TV</option>
					<option value="">Personal Video Recorder (PVR)</option>
					<option value="">3D Technology</option>
					<option value="">Upright Cleaner</option>
					<option value="">Cylinder Cleaner</option>
					<option value="">DECT Phone</option>
					<option value="">DECT Answer Phone</option>
					<option value="">Washing Machines</option>
					<option value="">Tumble Dryers</option>
					<option value="">Dishwashers</option>
					<option value="">Fridge-Freezers</option>
					<option value="">Freezers</option>
					<option value="">Refridgerators</option>
					<option value="">Microwave (Solo)</option>
					<option value="">Microwave (Grill)</option>
					<option value="">Microwave Combination</option>
					<option value="">Kettles</option>
					<option value="">Toasters</option>
					<option value="">Irons</option>
					<option value="">Breadmakers</option>
					<option value="">Microsystems</option>
					<option value="">Minisystems</option>
					<option value="">CD, Radio and Cassette Players</option>
					<option value="">Pure Radios</option>
					<option value="">Dimplex Fires</option>
					<option value="">Convector Heaters</option>
					<option value="">Fan Heaters</option>
					<option value="">Mens Shavers/Grooming</option>
					<option value="">Ladies Shavers/Beauty</option>
					<option value="">Straighteners</option>
					<option value="">Epilators</option>
					<option value="">Stylish Cameras</option>
					<option value="">Super Zoom Cameras</option>
					<option value="">SD Camcorders</option>
					<option value="">HD Camcorders</option>
					<option value="">HDD Camcorders</option>
					<option value="">Bluray Discs</option>
					<option value="">DVD Discs</option>
					<option value="">Leads</option>
					<option value="">Mini DV Tapes</option>
					<option value="">SD/SDHC/SDXC Cards</option>
			</select>
		</td>
		<td class="error"><?php = $errors['product'] ?></td>
	</tr>

	<tr>
            <td>Price:</td>
            <td><input id="price" type="text" size="30" name="price" value="<?php = htmlentities($values['price']) ?>"/></td>
            <td class="error"><?php = $errors['price'] ?></td>
        </tr>

	<tr>
            <td>Please upload an Image:</td>
            <td><input id="photo" type="file" size="30" name="photo" value="<?php = htmlentities($values['photo']) ?>"/></td>
            <td class="error"><?php = $errors['photo'] ?></td>
        </tr>

	 <tr>
            <td valign="top">Description:</td>
            <td>
                <textarea id="description" name="description" cols="30" rows="6"><?= htmlentities($values['description']) ?></textarea>
            </td>
            <td class="error"><?= $errors['description'] ?></td>
        </tr>


		<div>
			<input id="submmit" name="submit" type="submit" value="Send" />
		</div>

	</table>
	</form>
</div>
</body>
</html>

    <?php
}

function ProcessForm($values)
{
    //INSERT VARIABLES FROM INSERT_ADD.PHP
    
    // Replace with actual page or redirect 
    header ("Location: http://www.starjokes.com"); 
}

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $formValues = $_POST;
    $formErrors = array();
    
    if (!VerifyForm($formValues, $formErrors))
        DisplayForm($formValues, $formErrors);
    else
        ProcessForm($formValues);
}
else
    DisplayForm(null, null);
?>

Link to comment
https://forums.phpfreaks.com/topic/216425-php-form-errors/
Share on other sites

<?php = is not valid syntax. <?php echo is.

 

Don't use this: action="<?php echo $_SERVER['PHP_SELF'] ?>" as it presents an XSS vulnerability. To submit a form to itself, either use action="" or specifiy the name of the script.

Link to comment
https://forums.phpfreaks.com/topic/216425-php-form-errors/#findComment-1124894
Share on other sites

Im still getting a blank page even after correcting all the errors.

 

Here is my new code:

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1); ?>
<?php

function VerifyForm($values, $errors)
{
    // Do all necessary form verification

// Validate Model Number
    if (strlen($values['model']) < 4);{
        $errors['model'] = 'Model Number too short';}
    elseif (strlen($values['model']) > 50);{
	$errors['model'] = 'Model Number too long';}

// Validate Price
if (is_numeric($values['price']) == 0);{
        $errors['price'] = 'No Price has been entered';}

// Validate Product
if(($values['product'])== 'please_select');{
	$errors['product'] = 'No Product has been selected';}

// Validate Image
if ((($_FILES["photo"]["type"] != "image/gif");
	|| ($_FILES["photo"]["type"] != "image/jpeg");
	|| ($_FILES["photo"]["type"] != "image/pjpeg");
	&& ($_FILES["photo"]["size"] > 2000000)));{
	$errors['photo'] = 'Image format must be either JPG/JPEG/GIF or PNG';}

// Validate Description
if (strlen($values['description']) < 10);{
        $errors['description'] = 'Description is too short';}
        
    return (count($errors) == 0);
}

function DisplayForm($values, $errors)
{
    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>swiftelectrical.net - Test Area</title>
<link rel="stylesheet" href="test.css" type="text/css" media="screen" />
<style>
            TD.error
            {
                color: red;
                font-weight: bold;    
            }
    </style>
</head>
<body>

<div id="container">

	<h1>Add a Product</h1>

<?php
    if (count($errors) > 0);
        echo "<p>There were some errors in your submitted form, please correct them and try again.</p>";
    ?>

	<form method="post" id="customForm" action="" enctype="multipart/form-data">

<table>

        <tr>
            <td>Model Number:</td>
            <td><input id="model" type="text" size="30" name="model" value="<?php echo = htmlentities($values['model']); ?>"/></td>
            <td class="error"><?php echo = $errors['model']; ?></td>
        </tr>

	<tr>
		<td>Choose a Product:</td>
		<td>
			<select class="product" name="product">
				<option value="please_select">Please select an option below</option>
					<option value="1">19" LCD TV</option>
					<option value="2">22" LCD TV</option>
					<option value="3">26" LCD TV</option>
					<option value="4">32" LCD TV</option>
					<option value="5">37" LCD TV</option>
					<option value="6">42" LCD TV</option>
					<option value="7">37" Plasma TV</option>
					<option value="8">42" Plasma TV</option>
					<option value="9">46" Plasma TV</option>
					<option value="10">50" Plasma TV</option>
					<option value="11">54" Plasma TV</option>
					<option value="12">58" Plasma TV</option>
					<option value="13">Wall Bracket</option>
					<option value="14">Home Cinema System</option>
					<option value="15">Bluray Home Cinema System</option>
					<option value="16">DVD Recorder</option>
					<option value="17">DVD Player</option>
					<option value="18">DVD Portable</option>
					<option value="">Bluray Recorder</option>
					<option value="">Bluray Player</option>
					<option value="">Bluray Portable</option>
					<option value="">Projector</option>
					<option value="">37" LCD TV</option>
					<option value="">42" LCD TV</option>
					<option value="">Personal Video Recorder (PVR)</option>
					<option value="">3D Technology</option>
					<option value="">Upright Cleaner</option>
					<option value="">Cylinder Cleaner</option>
					<option value="">DECT Phone</option>
					<option value="">DECT Answer Phone</option>
					<option value="">Washing Machines</option>
					<option value="">Tumble Dryers</option>
					<option value="">Dishwashers</option>
					<option value="">Fridge-Freezers</option>
					<option value="">Freezers</option>
					<option value="">Refridgerators</option>
					<option value="">Microwave (Solo)</option>
					<option value="">Microwave (Grill)</option>
					<option value="">Microwave Combination</option>
					<option value="">Kettles</option>
					<option value="">Toasters</option>
					<option value="">Irons</option>
					<option value="">Breadmakers</option>
					<option value="">Microsystems</option>
					<option value="">Minisystems</option>
					<option value="">CD, Radio and Cassette Players</option>
					<option value="">Pure Radios</option>
					<option value="">Dimplex Fires</option>
					<option value="">Convector Heaters</option>
					<option value="">Fan Heaters</option>
					<option value="">Mens Shavers/Grooming</option>
					<option value="">Ladies Shavers/Beauty</option>
					<option value="">Straighteners</option>
					<option value="">Epilators</option>
					<option value="">Stylish Cameras</option>
					<option value="">Super Zoom Cameras</option>
					<option value="">SD Camcorders</option>
					<option value="">HD Camcorders</option>
					<option value="">HDD Camcorders</option>
					<option value="">Bluray Discs</option>
					<option value="">DVD Discs</option>
					<option value="">Leads</option>
					<option value="">Mini DV Tapes</option>
					<option value="">SD/SDHC/SDXC Cards</option>
			</select>
		</td>
		<td class="error"><?php echo = $errors['product']; ?></td>
	</tr>

	<tr>
            <td>Price:</td>
            <td><input id="price" type="text" size="30" name="price" value="<?php echo = htmlentities($values['price']); ?>"/></td>
            <td class="error"><?php echo = $errors['price']; ?></td>
        </tr>

	<tr>
            <td>Please upload an Image:</td>
            <td><input id="photo" type="file" size="30" name="photo" value="<?php echo = htmlentities($values['photo']); ?>"/></td>
            <td class="error"><?php echo = $errors['photo']; ?></td>
        </tr>

	 <tr>
            <td valign="top">Description:</td>
            <td>
                <textarea id="description" name="description" cols="30" rows="6"><?php echo = htmlentities($values['description']); ?></textarea>
            </td>
            <td class="error"><?php echo = $errors['description']; ?></td>
        </tr>


		<div>
			<input id="submmit" name="submit" type="submit" value="Send" />
		</div>

	</table>
	</form>
</div>
</body>
</html>

    <?php
}

function ProcessForm($values)
{
    //INSERT VARIABLES FROM INSERT_ADD.PHP
    
    // Replace with actual page or redirect 
    header ("Location: http://www.example.com"); 
}

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $formValues = $_POST;
    $formErrors = array();
    
    if (!VerifyForm($formValues, $formErrors))
        DisplayForm($formValues, $formErrors);
    else
        ProcessForm($formValues);
}
else
    DisplayForm(null, null);
?>

 

 

 

Im totally confused now, any more help

Link to comment
https://forums.phpfreaks.com/topic/216425-php-form-errors/#findComment-1124907
Share on other sites

You have a bunch of semicolons in places where they don't belong, and still have the equals signs where it doesn't belong, after every <?php echo. You should also turn on error reporting in your php.ini file so these problems are displayed to you.

 

This now has no parse errors, but obviously I didn't test it for functionality.

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1); ?>
<?php
function VerifyForm($values, $errors) { 
// Do all necessary form verification
// Validate Model Number
if (strlen($values['model']) < 4) {
	$errors['model'] = 'Model Number too short';
} elseif( strlen($values['model']) > 50); {
		$errors['model'] = 'Model Number too long';}
		// Validate Price
		if (is_numeric($values['price']) == 0){
			$errors['price'] = 'No Price has been entered';}
			// Validate Product
			if(($values['product'])== 'please_select');{
				$errors['product'] = 'No Product has been selected';}
				// Validate Image
				if ((($_FILES["photo"]["type"] != "image/gif")
				|| ($_FILES["photo"]["type"] != "image/jpeg")
				|| ($_FILES["photo"]["type"] != "image/pjpeg")
				&& ($_FILES["photo"]["size"] > 2000000))) {
					$errors['photo'] = 'Image format must be either JPG/JPEG/GIF or PNG';}
					// Validate Description
					if (strlen($values['description']) < 10);{
						$errors['description'] = 'Description is too short';}
						return (count($errors) == 0);
}
function DisplayForm($values, $errors)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>swiftelectrical.net - Test Area</title>
<link rel="stylesheet" href="test.css" type="text/css" media="screen" />
<style>
TD.error
{
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<div id="container">
<h1>Add a Product</h1>
<?php
if (count($errors) > 0);
echo "<p>There were some errors in your submitted form, please correct them and try again.</p>";
?>
<form method="post" id="customForm" action="" enctype="multipart/form-data">
<table>
<tr>
<td>Model Number:</td>
<td><input id="model" type="text" size="30" name="model" value="<?php echo htmlentities($values['model']); ?>"/></td>
<td class="error"><?php echo $errors['model']; ?></td>
</tr>
<tr>
<td>Choose a Product:</td>
<td>
<select class="product" name="product">
<option value="please_select">Please select an option below</option>
<option value="1">19" LCD TV</option>
<option value="2">22" LCD TV</option>
<option value="3">26" LCD TV</option>
<option value="4">32" LCD TV</option>
<option value="5">37" LCD TV</option>
<option value="6">42" LCD TV</option>
<option value="7">37" Plasma TV</option>
<option value="8">42" Plasma TV</option>
<option value="9">46" Plasma TV</option>
<option value="10">50" Plasma TV</option>
<option value="11">54" Plasma TV</option>
<option value="12">58" Plasma TV</option>
<option value="13">Wall Bracket</option>
<option value="14">Home Cinema System</option>
<option value="15">Bluray Home Cinema System</option>
<option value="16">DVD Recorder</option>
<option value="17">DVD Player</option>
<option value="18">DVD Portable</option>
<option value="">Bluray Recorder</option>
<option value="">Bluray Player</option>
<option value="">Bluray Portable</option>
<option value="">Projector</option>
<option value="">37" LCD TV</option>
<option value="">42" LCD TV</option>
<option value="">Personal Video Recorder (PVR)</option>
<option value="">3D Technology</option>
<option value="">Upright Cleaner</option>
<option value="">Cylinder Cleaner</option>
<option value="">DECT Phone</option>
<option value="">DECT Answer Phone</option>
<option value="">Washing Machines</option>
<option value="">Tumble Dryers</option>
<option value="">Dishwashers</option>
<option value="">Fridge-Freezers</option>
<option value="">Freezers</option>
<option value="">Refridgerators</option>
<option value="">Microwave (Solo)</option>
<option value="">Microwave (Grill)</option>
<option value="">Microwave Combination</option>
<option value="">Kettles</option>
<option value="">Toasters</option>
<option value="">Irons</option>
<option value="">Breadmakers</option>
<option value="">Microsystems</option>
<option value="">Minisystems</option>
<option value="">CD, Radio and Cassette Players</option>
<option value="">Pure Radios</option>
<option value="">Dimplex Fires</option>
<option value="">Convector Heaters</option>
<option value="">Fan Heaters</option>
<option value="">Mens Shavers/Grooming</option>
<option value="">Ladies Shavers/Beauty</option>
<option value="">Straighteners</option>
<option value="">Epilators</option>
<option value="">Stylish Cameras</option>
<option value="">Super Zoom Cameras</option>
<option value="">SD Camcorders</option>
<option value="">HD Camcorders</option>
<option value="">HDD Camcorders</option>
<option value="">Bluray Discs</option>
<option value="">DVD Discs</option>
<option value="">Leads</option>
<option value="">Mini DV Tapes</option>
<option value="">SD/SDHC/SDXC Cards</option>
</select>
</td>
<td class="error"><?php echo $errors['product']; ?></td>
</tr>
<tr>
<td>Price:</td>
<td><input id="price" type="text" size="30" name="price" value="<?php echo htmlentities($values['price']); ?>"/></td>
<td class="error"><?php echo $errors['price']; ?></td>
</tr>
<tr>
<td>Please upload an Image:</td>
<td><input id="photo" type="file" size="30" name="photo" value="<?php echo htmlentities($values['photo']); ?>"/></td>
<td class="error"><?php echo $errors['photo']; ?></td>
</tr>
<tr>
<td valign="top">Description:</td>
<td>
<textarea id="description" name="description" cols="30" rows="6"><?php echo htmlentities($values['description']); ?></textarea>
</td>
<td class="error"><?php echo $errors['description']; ?></td>
</tr>
<div>
<input id="submmit" name="submit" type="submit" value="Send" />
</div>
</table>
</form>
</div>
</body>
</html>
<?php
}
function ProcessForm($values)
{
//INSERT VARIABLES FROM INSERT_ADD.PHP
// Replace with actual page or redirect 
header ("Location: http://www.example.com");
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$formValues = $_POST;
$formErrors = array();
if (!VerifyForm($formValues, $formErrors))
DisplayForm($formValues, $formErrors);
else
ProcessForm($formValues);
}
else
DisplayForm(null, null);
?>

Link to comment
https://forums.phpfreaks.com/topic/216425-php-form-errors/#findComment-1124909
Share on other sites

If you attempt to use the value of a variable before it's been defined, you'll get the 'Undefined' warning. To take care of those, you need to check for its presence before using it.

 

Causes 'undefined index: email' warning if the form hasn't been submitted:

$email = $_POST['email'];

 

Checks for the value before trying to use it, so no warning:

if( isset($_POST['email']) ) {
     $email = $_POST['email'];
} else {
     $email = '';
}

// Can also be done "shorthand" using ternary syntax

$email = isset($_POST['email']) ? $_POST['email'] : '';

Link to comment
https://forums.phpfreaks.com/topic/216425-php-form-errors/#findComment-1124922
Share on other sites

I normally don't go through an entire script for someone like this, bu I know you've been having problems with it for some time now. The warnings are now handled, but I didn't go through the validation functions. You'll have to test those and see if they do what you intended.

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1); ?>
<?php
function VerifyForm($values, $errors) {
   // Do all necessary form verification
   // Validate Model Number
   if (strlen($values['model']) < 4) {
      $errors['model'] = 'Model Number too short';
   } elseif( strlen($values['model']) > 50); {
         $errors['model'] = 'Model Number too long';}
         // Validate Price
         if (is_numeric($values['price']) == 0){
            $errors['price'] = 'No Price has been entered';}
            // Validate Product
            if(($values['product'])== 'please_select');{
               $errors['product'] = 'No Product has been selected';}
               // Validate Image
               if ((($_FILES["photo"]["type"] != "image/gif")
               || ($_FILES["photo"]["type"] != "image/jpeg")
               || ($_FILES["photo"]["type"] != "image/pjpeg")
               && ($_FILES["photo"]["size"] > 2000000))) {
                  $errors['photo'] = 'Image format must be either JPG/JPEG/GIF or PNG';}
                  // Validate Description
                  if (strlen($values['description']) < 10);{
                     $errors['description'] = 'Description is too short';}
                     return (count($errors) == 0);
}
function DisplayForm($values, $errors)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>swiftelectrical.net - Test Area</title>
<link rel="stylesheet" href="test.css" type="text/css" media="screen" />
<style>
TD.error
{
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<div id="container">
<h1>Add a Product</h1>
<?php
if (count($errors) > 0);
echo "<p>There were some errors in your submitted form, please correct them and try again.</p>";
?>
<form method="post" id="customForm" action="" enctype="multipart/form-data">
<table>
<tr>
<td>Model Number:</td>
<td><input id="model" type="text" size="30" name="model" value="<?php echo htmlentities($values['model']); ?>"/></td>
<td class="error"><?php echo isset($errors['model']) ? $errors['model'] : ''; ?></td>
</tr>
<tr>
<td>Choose a Product:</td>
<td>
<select class="product" name="product">
<option value="please_select">Please select an option below</option>
<option value="1">19" LCD TV</option>
<option value="2">22" LCD TV</option>
<option value="3">26" LCD TV</option>
<option value="4">32" LCD TV</option>
<option value="5">37" LCD TV</option>
<option value="6">42" LCD TV</option>
<option value="7">37" Plasma TV</option>
<option value="8">42" Plasma TV</option>
<option value="9">46" Plasma TV</option>
<option value="10">50" Plasma TV</option>
<option value="11">54" Plasma TV</option>
<option value="12">58" Plasma TV</option>
<option value="13">Wall Bracket</option>
<option value="14">Home Cinema System</option>
<option value="15">Bluray Home Cinema System</option>
<option value="16">DVD Recorder</option>
<option value="17">DVD Player</option>
<option value="18">DVD Portable</option>
<option value="">Bluray Recorder</option>
<option value="">Bluray Player</option>
<option value="">Bluray Portable</option>
<option value="">Projector</option>
<option value="">37" LCD TV</option>
<option value="">42" LCD TV</option>
<option value="">Personal Video Recorder (PVR)</option>
<option value="">3D Technology</option>
<option value="">Upright Cleaner</option>
<option value="">Cylinder Cleaner</option>
<option value="">DECT Phone</option>
<option value="">DECT Answer Phone</option>
<option value="">Washing Machines</option>
<option value="">Tumble Dryers</option>
<option value="">Dishwashers</option>
<option value="">Fridge-Freezers</option>
<option value="">Freezers</option>
<option value="">Refridgerators</option>
<option value="">Microwave (Solo)</option>
<option value="">Microwave (Grill)</option>
<option value="">Microwave Combination</option>
<option value="">Kettles</option>
<option value="">Toasters</option>
<option value="">Irons</option>
<option value="">Breadmakers</option>
<option value="">Microsystems</option>
<option value="">Minisystems</option>
<option value="">CD, Radio and Cassette Players</option>
<option value="">Pure Radios</option>
<option value="">Dimplex Fires</option>
<option value="">Convector Heaters</option>
<option value="">Fan Heaters</option>
<option value="">Mens Shavers/Grooming</option>
<option value="">Ladies Shavers/Beauty</option>
<option value="">Straighteners</option>
<option value="">Epilators</option>
<option value="">Stylish Cameras</option>
<option value="">Super Zoom Cameras</option>
<option value="">SD Camcorders</option>
<option value="">HD Camcorders</option>
<option value="">HDD Camcorders</option>
<option value="">Bluray Discs</option>
<option value="">DVD Discs</option>
<option value="">Leads</option>
<option value="">Mini DV Tapes</option>
<option value="">SD/SDHC/SDXC Cards</option>
</select>
</td>
<td class="error"><?php echo isset($errors['product']) ? $errors['product'] : ''; ?></td>
</tr>
<tr>
<td>Price:</td>
<td><input id="price" type="text" size="30" name="price" value="<?php echo isset($values['price']) ? $values['price'] : htmlentities($values['price']) ; '';?>"/></td>
<td class="error"><?php echo isset($errors['price']) ? $errors['price'] : ''; ?></td>
</tr>
<tr>
<td>Please upload an Image:</td>
<td><input id="photo" type="file" size="30" name="photo" value="<?php echo isset($values['photo']) ? htmlentities($values['photo']) : ''; ?>"/></td>
<td class="error"><?php echo isset($errors['photo']) ? $errors['photo'] : ''; ?></td>
</tr>
<tr>
<td valign="top">Description:</td>
<td>
<textarea id="description" name="description" cols="30" rows="6"><?php echo isset($values['description']) ? htmlentities($values['description']) : ''; ?></textarea>
</td>
<td class="error"><?php echo isset($errors['description']) ? $errors['description'] : ''; ?></td>
</tr>
<div>
<input id="submmit" name="submit" type="submit" value="Send" />
</div>
</table>
</form>
</div>
</body>
</html>
<?php
}
function ProcessForm($values)
{
   //INSERT VARIABLES FROM INSERT_ADD.PHP
   // Replace with actual page or redirect 
   header ("Location: http://www.example.com");
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
   $formValues = $_POST;
   $formErrors = array();
   if (!VerifyForm($formValues, $formErrors))
   DisplayForm($formValues, $formErrors);
   else
   ProcessForm($formValues);
}
else
DisplayForm(null, null);
?>

Link to comment
https://forums.phpfreaks.com/topic/216425-php-form-errors/#findComment-1124996
Share on other sites

Okay, Thankyou, would you be able to check in the next couple of days?  ;D

 

Thanks for your help!

 

The code in my last post has been edited so the 'undefined' warnings should be taken care of now . . .

Link to comment
https://forums.phpfreaks.com/topic/216425-php-form-errors/#findComment-1125073
Share on other sites

@Pikachu2000

 

Thankyou for all your help and i understand fully you may have a busy schedule.

 

Im still faced with a few errors. I have managed to get the validation errors on screen, there were two missing & symbols. Everything works validation wise, but when i choose a value from the dropdown menu, the variable doesn't save and returns back to default, also when i upload an image to test validation, it always throws back the error.

 

Any ideas or recommendations?

 

I will post the code again with changes....

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1); ?>
<?php
function VerifyForm(&$values, &$errors) {
   // Do all necessary form verification
   // Validate Model Number
   if (strlen($values['model']) < 4) {
      $errors['model'] = 'Model Number too short';
   } elseif( strlen($values['model']) > 20) {
         $errors['model'] = 'Model Number too long';}
         // Validate Price
         if (is_numeric($values['price']) == 0){
            $errors['price'] = 'No Price has been entered';}
            // Validate Product
            if(($values['product']) == 0){
               $errors['product'] = 'No Product has been selected';}
               // Validate Image
               if ((($_FILES["photo"]["type"] != "image/gif")
               || ($_FILES["photo"]["type"] != "image/jpeg")
               || ($_FILES["photo"]["type"] != "image/pjpeg")
               && ($_FILES["photo"]["size"] > 2000000))) {
                  $errors['photo'] = 'Image format must be either JPG/JPEG/GIF or PNG';}
                  // Validate Description
                  if (strlen($values['description']) == 0){
                     $errors['description'] = 'Description is too short';}
                     return (count($errors) == 0);
}
function DisplayForm($values, $errors)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>swiftelectrical.net - Test Area</title>
<link rel="stylesheet" href="test.css" type="text/css" media="screen" />
<style>
TD.error
{
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<div id="container">
<h1>Add a Product</h1>
<?php
if (count($errors) > 0);
echo "<p>There were some errors in your submitted form, please correct them and try again.</p>";
?>
<form method="post" id="customForm" action="" enctype="multipart/form-data">
<table>
<tr>
<td>Model Number:</td>
<td><input id="model" type="text" size="30" name="model" value="<?php echo htmlentities($values['model']); ?>"/></td>
<td class="error"><?php echo isset($errors['model']) ? $errors['model'] : ''; ?></td>
</tr>
<tr>
<td>Choose a Product:</td>
<td>
<select class="product" name="product">
<option value="0">Please select an option below</option>
<option value="1">19" LCD TV</option>
<option value="2">22" LCD TV</option>
<option value="3">26" LCD TV</option>
<option value="4">32" LCD TV</option>
<option value="5">37" LCD TV</option>
<option value="6">42" LCD TV</option>
<option value="7">37" Plasma TV</option>
<option value="8">42" Plasma TV</option>
<option value="9">46" Plasma TV</option>
<option value="10">50" Plasma TV</option>
<option value="11">54" Plasma TV</option>
<option value="12">58" Plasma TV</option>
<option value="13">Wall Bracket</option>
<option value="14">Home Cinema System</option>
<option value="15">Bluray Home Cinema System</option>
<option value="16">DVD Recorder</option>
<option value="17">DVD Player</option>
<option value="18">DVD Portable</option>
<option value="">Bluray Recorder</option>
<option value="">Bluray Player</option>
<option value="">Bluray Portable</option>
<option value="">Projector</option>
<option value="">37" LCD TV</option>
<option value="">42" LCD TV</option>
<option value="">Personal Video Recorder (PVR)</option>
<option value="">3D Technology</option>
<option value="">Upright Cleaner</option>
<option value="">Cylinder Cleaner</option>
<option value="">DECT Phone</option>
<option value="">DECT Answer Phone</option>
<option value="">Washing Machines</option>
<option value="">Tumble Dryers</option>
<option value="">Dishwashers</option>
<option value="">Fridge-Freezers</option>
<option value="">Freezers</option>
<option value="">Refridgerators</option>
<option value="">Microwave (Solo)</option>
<option value="">Microwave (Grill)</option>
<option value="">Microwave Combination</option>
<option value="">Kettles</option>
<option value="">Toasters</option>
<option value="">Irons</option>
<option value="">Breadmakers</option>
<option value="">Microsystems</option>
<option value="">Minisystems</option>
<option value="">CD, Radio and Cassette Players</option>
<option value="">Pure Radios</option>
<option value="">Dimplex Fires</option>
<option value="">Convector Heaters</option>
<option value="">Fan Heaters</option>
<option value="">Mens Shavers/Grooming</option>
<option value="">Ladies Shavers/Beauty</option>
<option value="">Straighteners</option>
<option value="">Epilators</option>
<option value="">Stylish Cameras</option>
<option value="">Super Zoom Cameras</option>
<option value="">SD Camcorders</option>
<option value="">HD Camcorders</option>
<option value="">HDD Camcorders</option>
<option value="">Bluray Discs</option>
<option value="">DVD Discs</option>
<option value="">Leads</option>
<option value="">Mini DV Tapes</option>
<option value="">SD/SDHC/SDXC Cards</option>
</select>
</td>
<td class="error"><?php echo isset($errors['product']) ? $errors['product'] : ''; ?></td>
</tr>
<tr>
<td>Price:</td>
<td><input id="price" type="text" size="30" name="price" value="<?php echo isset($values['price']) ? $values['price'] : htmlentities($values['price']) ; '';?>"/></td>
<td class="error"><?php echo isset($errors['price']) ? $errors['price'] : ''; ?></td>
</tr>
<tr>
<td>Please upload an Image:</td>
<td><input id="photo" type="file" size="30" name="photo" value="<?php echo isset($values['photo']) ? htmlentities($values['photo']) : ''; ?>"/></td>
<td class="error"><?php echo isset($errors['photo']) ? $errors['photo'] : ''; ?></td>
</tr>
<tr>
<td valign="top">Description:</td>
<td>
<textarea id="description" name="description" cols="30" rows="6"><?php echo isset($values['description']) ? htmlentities($values['description']) : ''; ?></textarea>
</td>
<td class="error"><?php echo isset($errors['description']) ? $errors['description'] : ''; ?></td>
</tr>
<tr>
<td><input id="submmit" name="submit" type="submit" value="Send" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
<?php
}
function ProcessForm($values)
{
   //INSERT VARIABLES FROM INSERT_ADD.PHP
   // Replace with actual page or redirect 
   header ("Location: http://www.example.com");
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
   $formValues = $_POST;
   $formErrors = array();
   if (!VerifyForm($formValues, $formErrors))
   DisplayForm($formValues, $formErrors);
   else
   ProcessForm($formValues);
}
else
DisplayForm(null, null);
?>

 

Link to comment
https://forums.phpfreaks.com/topic/216425-php-form-errors/#findComment-1125188
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.