Jump to content

PHP Validation Form


aleister1987

Recommended Posts

Hi all,

 

Im trying to add validation to a form to allow PHP to check before its sends the values to another page for them to be uploaded to a database.

 

The problem i have is that i have taken a, i thought simple PHP validating script off the net and i cant seem to get it to work, its just giving me a blank page. I dont know if the script is outdated, i dont think it is.

 

The script is here:

 

<?php
include("validation.php");
?>

<!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>Test Area</title>
<link rel="stylesheet" href="test.css" type="text/css" media="screen" />
</head>
<body>

<div id="container">

	<h1>Add a Product</h1>

	<?if(isset($_POST['send'])&amp;amp;amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp;amp;amp;(!validateModel($_POST['model']) || !validatePrice($_POST['price']) || !validateDescription($_POST['description']) ) ):?>
			<div id="error">
				<ul>
					<?if(!validateModel($_POST['model'])):?>
						<li><strong>Invalid Name:</strong> We want names with more than 3 letters!</li>
					<?endif?>
					<?if(!validatePrice($_POST['price'])):?>
						<li><strong>Invalid E-mail:</strong> Type a Valid Price!</li>
					<?endif?>
					<?if(!validateDescription($_POST['description'])):?>
						<li><strong>Invalid message:</strong> Type a message with at least with 10 letters</li>
					<?endif?>
				</ul>
			</div>
		<?elseif(isset($_POST['send'])):?>
			<div id="error" class="valid">
				<ul>
					<li><strong>Congratulations!</strong> All fields are OK </li>
				</ul>
			</div>
	<?endif?>

	<form method="post" id="customForm" action="" enctype="multipart/form-data">
		<div>
			<label for="model">Model Number</label>
			<input id="model" name="model" type="text" />
			<span id="modelInfo">Please enter your Product Model Number!</span>
		</div>

		<div>
			<label for="product">Product</label>

				<select class="products" name="products">
					<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>

			<span id="productInfo">Please choose a Product Title!</span>
		</div>

		<div>
			<label for="price">Price</label>
			<input id="price" name="price" type="text" />
			<span id="priceInfo">Please enter a Price!</span>
		</div>

		<div>
			<label for="photo">Image</label>
			<input style="font-size:13px;" class="file" name="photo" type="file" />
			<span id="photoInfo">Please choose an Image!</span>
		</div>

		<div>
			<label for="message">Description</label>
			<textarea id="description" name="description" cols="" rows=""></textarea>
		</div>

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

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

 

 

This is the validation file:

 

<?php
function validateModel($model){
	//if it's NOT valid
	if(strlen($model) < 4)
		return false;
	//if it's valid
	else
		return true;
}

function validatePrice($price){
	//if it's NOT valid
	if(strlen($price) < 4)
		return false;
	//if it's valid
	else
		return true;
}

function validateDescription($description){
	//if it's NOT valid
	if(strlen($description) < 10)
		return false;
	//if it's valid
	else
		return true;
}
?>

 

 

I can't spot the problem, any suggestions?

Link to comment
Share on other sites

This line is causing a parse error because you're attempting to echo a bunch of ampersands without actually issuing an echo. You should also get rid of the short open <? syntax, and use the full <?php open tags.

 

<?php if(isset($_POST['send'])&amp;amp;amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp;amp;amp;(!validateModel($_POST['model']) || !validatePrice($_POST['price']) || !validateDescription($_POST['description']) ) ): ?>

Link to comment
Share on other sites

I don't really see why you would get a blank page; but here are a couple of points to help discover the problem.

 

1) When you get a blank page, use the 'View Source' feature of the browser to see if there is anything being sent that is not visible. This might help isolate the problem.

 

2) Put the following lines at the top of your script - immediately after the openning PHP tag:

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

this should show any errors that occur which might be preventing the script from working.

 

3) Change all of your short tags to full tags. Using short tags is a bad habit and can lead to problems - not all servers support short tags.  For instance:

// THIS LINE OF CODE ...
<?elseif(isset($_POST['send'])):?>

// ... SHOULD ACTUALLY BE ...
<?php elseif(isset($_POST['send'])):?>

 

4) That big IF statement looks hinky:

<?if(isset($_POST['send'])&amp;amp;amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp;amp;amp;(!validateModel($_POST['model']) || !validatePrice($_POST['price']) || !validateDescription($_POST['description']) ) ):?>

What are all those amp;'s in there? that should probably be:

<?php if(isset($_POST['send']) AND 
  (!validateModel($_POST['model']) || !validatePrice($_POST['price']) 
    || !validateDescription($_POST['description']) ) ):?>

I'm guessing that line originally was if ( ... && ...) but has been run through htmlspecialchars() (or something) about 9 times. Which makes me wonder about the integrity of the script.  At any rate, I use AND instead of && and I use OR instead of ||. I just think it is easier to read.  Mixing them may not be a good idea though, they do have different levels of precedence.

Link to comment
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.