1 Replies - 3973 Views - Last Post: 22 November 2012 - 06:59 PM Rate Topic: -----

#1 ettedo2000   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 26-September 12

Need help with validating input on a self-referential php program

Posted 22 November 2012 - 06:19 PM

Hello, I need some help with this homework. I got it all working except of one wired problem.

I am writing a php form where the user inputs a city or state or zipcode. At least one field has to be entered. If no fields are filled, an error message is outputted with a new form. If everything is right than it will sent a query to the database.

I have everything working including the query. my problem is in the validation of user input. While it validates the city and state, when I enter a 6 digit zipcode it is not outputting an error message but going to the SQL query. I need it to only accept a 5 digit zipcode. Also it is not striping the tags even though I have exactly the same code in my program as for the state and city and they work. I have been working on this for hours and I really don't know what I am doing wrong.

I hope that someone maybe can tell me where I am going wrong.

Here is my code
<!DOCTYPE HTML >
<?php

$formproc='n';
$cityerror = '';
$stateerror = '';
$ziperror= '';

if(isset($_GET['city']) || isset($_GET['state']) || isset($_GET['zip']))
	{
	$city= $_GET['city'];
	$city=	trim($_GET['city']); 
	$city = ucfirst(strtolower($city));
	$city = strip_tags($city);
	
	$state = $_GET['state'];
	$state = ucfirst(strtolower($state));
	$state = strip_tags($state);
	$state=	trim($_GET['state']); 
	
	$zip = $_GET['zip'];
	$zip = strip_tags($zip);
	$zip = trim($_GET['zip']);
	
	if($city)
		{
		if(strlen($city) <2 or strlen($city) >20)													//If city name is to long
			{
			$cityerror = '*';																		//showes which form has incorect data
			$city = ''; 																			//will display empty space wher inpuut is incorect
			echo "<b><font color=red>* City name has to be between 2 and 20 charachters</font></b> ";
			}
		else if(is_numeric($city))																//if numeric displayes error message
			{ 
			$cityerror = '*';
			$city='';
			echo "<b> Wrong input, you entred a numeric value</b> ";	
			}
		}
	if($state)
		{
			if(strlen($state)!=2)																		//if to long displayes error message
			{ 
			$stateerror = '*';
			$state='';
			echo "<b> Enter only the states abbreviations</b> ";
			}
			else if(is_numeric($state))																//if numeric dispaly error message
			{ 
			$stateerror = '*';
			$state='';
			echo "<b> Wrong input, you entred a numeric value</b> ";			
			}
		}
	if($zip)
		{
			if(!is_numeric($zip)) 																		//if numeric displayes error message
			{
			$ziperror = '*';
			$zip='';
			echo "<b><Wrong input, you did not entred a numeric value</b> ";
			}
			else if(strlen($zip)!=5)
			{
			$ziperror = '*';
			$zip='';
			echo "<b>You can only usa a 5 digit Zipcode</b>";
			}
		}
	
	if(empty($_GET['city']) && empty($_GET['state']) && empty($_GET['zip']))
			{	
			$cityerror = '*';				//showes which form has incorect data
			$stateerror = '*';
			$ziperror = '*';
			$city = ''; 				//will display empty space wher inpuut is incorect
			$state = '';
			$zip = '';
			echo "<b>Please enter one of the fields:City, State or Zipcode</b> ";
			}	
	$formproc="y";			
	}
	else
	{
	$city= "";
	$state="";
	$zip="";
	}
		
	if ($formproc=="y" and $cityerror !='*' and $stateerror !='*' and $zip !='*')
	{
	header("location:http://localhost/classphp/Lab_PAGE.php?city=$city&state=$state&zip=$zip");
	}
?>

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


<p> PLEASE ENTER A CITY , STATE OR ZIPCODE</p>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="get">								
<p>City: <input type="text" name="city" id="city" value="<?php echo $city;?>"/> <?php echo $cityerror;?></p>
<p>State: <input type="text" name="state" id="state" value="<?php echo $state;?>"/> <?php echo $stateerror;?></p>
<p>Zipcode: <input type="int" name="zip" id="zip" size="10" value="<?php echo $zip;?>"/> <?php echo $ziperror;?></p>
<br/><br/>	
<input type ="submit" name="submit" id="Submit">

</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with validating input on a self-referential php program

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,998
  • Joined: 08-August 08

Re: Need help with validating input on a self-referential php program

Posted 22 November 2012 - 06:59 PM

That code makes my eyes hurt. You seem to be under the impression that PHP and HTML are the same language. They're not. They don't even get processed by the same computer!

Finish this:
<?php
$form=<<<HERE
<form method="get">                            
<p>City: <input type="text" name="city" id="city" value="{city}"/> {cityerror}</p>
<p>State: <input type="text" name="state" id="state" value="{state}"/> {stateerror}</p>
<p>Zipcode: <input type="int" name="zip" id="zip" size="10" value="{zip}"/> {ziperror}</p>
<br/><br/> 
<input type ="submit" name="submit" id="Submit">
</form>
HERE;

$errors = array();
if(isset($_GET['city'])) {
	$has_errors = validate_data($_GET, $errors);
} else {
	$has_errors = false;
}

if(isset($_GET['city']) && !$has_errors) {
	send_query_to_database();
} else {
	show_form($form, $_GET, $errors);
}


function show_form($form, $data, $errs) {
	// There are 2 lines missing here.
	echo str_replace($replace, $with, $form);
}

function validate_data($data, &$err) {
 /* Your code here! */
 // I've removed 10 lines of my code. You could do it in less.
}
?>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1