Grodo Posted May 7, 2008 Share Posted May 7, 2008 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]; } } ?> Quote Link to comment Share on other sites More sharing options...
947740 Posted May 7, 2008 Share Posted May 7, 2008 On a side note, instead of doing += 1, you can just do ++. Could you try this?: if($varname == "") { //$varname is NULL } Quote Link to comment Share on other sites More sharing options...
Grodo Posted May 7, 2008 Author Share Posted May 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
Grodo Posted May 7, 2008 Author Share Posted May 7, 2008 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'; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.