Jump to content

Problem with $POST Validation Fuction


Grodo

Recommended Posts

Hello,

 

    I would like to thank you ahead of time for looking at this post.

 

I have a HTML form that has fields for First Name, Last Name, ect.  The fields are named respectively to their variables (fname,lname, ect...).  After the user submits the fields through post method the fields go through validation.  The validation function is called NULLcheck().  This function takes each field and checks for a NULL value.  If a NULL value is found it triggers the errored flag.  All errors are stored in an array. The function returns the error to the $listError array when an error is detected.  I have written many checks for this code and I have came to the conlusion the it does go through the NULLcheck() function but for some reason it does not assign values to the global variables.  It also seems to me that the fuction keeps validating as true and never triggers the errored flag.  I have attached my validating code below.

 

Thanks for  your time and helping me with this project,

Grodo

 


<?php

//Define Globals

global $error;
global $postVar;
global $errored;
global $fname;
global $lname;
global $address1;
global $city;
global $state;
global $country;
global $zip;

function NULLcheck() {

	// Define Errors
	$error[fname] = "Please enter your first name";
	$error[lname] = "Please enter your last name";
	$error[address1] = "Please enter a valid address";
	$error[city]  = "Please enter a valid city";
	$error[state] = "Please enter a valid state or territory";
	$error[country] = "Please enter a valid country";
	$error[zip] = "Please enter a valid postal code";
	// Define Variables
	$postVar[0] = "fname";
	$postVar[1] = "lname";
	$postVar[2] = "address1";
	$postVar[3] = "city";
	$postVar[4] = "state";
	$postVar[5] = "country";
	$postVar[6] = "zip";
	//Check for Null Variables
	for ( $counter = 0; $counter <= 6; $counter += 1) {
		if ($_POST[$postVar[$counter]] != NULL) {
			$fname = $_POST[$postVar[0]];
			$lname = $_POST[$postVar[1]];
			$address1 = $_POST[$postVar[2]];
			$city = $_POST[$postVar[3]];
			$state = $_POST[$postVar[4]];
			$country = $_POST[$postVar[5]];
			$zip = $_POST[$postVar[6]];
		}
	// If Null Values are found report error
		else {
			$errored = TRUE;
			return $error[$postVar[$counter]];
		}
	}
}	
$listError[] = NULLcheck();
//  If Errored = Flase then No Errors are found
             if (!$errored)	{
	echo 'No Errors';
}
else {
	for ($i = 0; $i <=6; $i += 1) {
		echo $listError[$i];
	}
}

?>


Link to comment
Share on other sites

Thanks for the reply 947740,

 

The if statement still doesnt work.  From what I seen it did trip the errored once... but after i reloaded it.  It did not trip the errored flag.  Also non of the values are setting I belive its a problem with  $fname = $_POST[$postVar[0]];

However logically it seems correct.  I got it working with a IF statement for each var but I was trying to make the code more effiecent and incorperate a loop.

Link to comment
Share on other sites

I got it working on extemely inefficent code but if some one can figure out how to incorperate a function with a loop that would be awesome for the time being im just gonna be using the code posted below.... Who knows it might help some one out.

 

<?php
//  Database info
//  Database: xxx_1234
//  User:  xxx_5678
//  Passwd: xxpassxx
//  Table Name: xtable
//  MYSQL DB Host:  127.0.0.0

//Define Globals
global $error;
global $postVar;
global $errored;
global $fname;
global $lname;
global $address1;
global $city;
global $state;
global $country;
global $zip;

// Connect to the database server
$dbcnx = @mysql_connect('127.0.0.0', 'xxx_5678', 'xxpassxx');
if (!$dbcnx) {
	exit('<p>Unable to connect to the ' .
	'database server at this time.</p>');
}
// Select the Catalog request database
if (!@mysql_select_db('xxx_1234')) {
	exit('<p>Unable to locate Catalog Request ' .
	'database at this time.</p>');
}

if ($_POST['company'] != NULL) {
	$company = $_POST['company'];
	$sql = "INSERT INTO xtable SET company='$company'";
	mysql_query($sql);
	echo 'Successfully wrote' . $company .'to Database';
}
else {
	include 'catalog.php';
}
?>	

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.