NETSYNC Posted November 2, 2011 Share Posted November 2, 2011 I am working on a script for a simple form with only 2 options that are dropdowns. I need to validate these two options that there is a selection made. I have gotten the first one to validate, but I cannot get the second one to validate. Can anyone steer me in the right direciton why only one is working? I get no errors in the script, so I assume I am just missing something. <?php // options for drop-down menu $choices = array('-- Choose Your Item','Anniversary Jacket', 'Anniversary T-Shirt'); $sizes = array('-- Choose Your Size','L', 'XL'); if($_SERVER['REQUEST_METHOD'] == 'GET'){ // display form when GET showForm(array()); } else{ // process form if POST $errors = validateForm(); if(count($errors)) showForm($errors); // if errors show again else print 'Form submitted succesfully!'; // no errors } // function generating form function showForm($errors){ global $choices,$sizes; // set defaults $defaults = array(); foreach($choices as $key => $choice){ if(isset($_POST['item']) && ($_POST['item'] == $key)) $defaults['item'][$key] = 'selected'; else $defaults['item'][$choice] = ''; } foreach($sizes as $key => $size){ if(isset($_POST['size']) && ($_POST['size'] == $key)) $defaults['size'][$key] = 'selected'; else $defaults['size'][$size] = ''; } // print form print "<form action='{$_SERVER['SCRIPT_NAME']}' method='post'>"; print "<div>"; print "<select name='item'>"; foreach($choices as $key => $choice){ print "<option value='{$key}' {$defaults['item'][$key]}>{$choice}</option>"; } print "</select>"; showError('item', $errors); print "</div>"; print "<div>"; print "<select name='size'>"; foreach($sizes as $key => $size){ print "<option value='{$key}' {$defaults['size'][$key]}>{$size}</option>"; } print "</select>"; showError('size', $errors); print "</div>"; print "<input type='submit'/>"; print "</form>"; } // display error function showError($type, $errors){ if(isset($errors[$type])) print "<b>{$errors[$type]}</b>"; } // validate data function validateForm(){ global $choices,$sizes; // start validation and store errors $error = array(); // validate drop-down if(!(isset($_POST['item']) && (array_key_exists($_POST['item'], $choices)) && $_POST['item'] != 0)) $errors['item'] = 'Select Item'; return $errors; // validate drop-down if(!(isset($_POST['size']) && (array_key_exists($_POST['size'], $choices)) && $_POST['size'] != 0)) $errors['size'] = 'Select Size'; return $errors; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250264-got-one-item-to-validate-but-not-two/ Share on other sites More sharing options...
xtopolis Posted November 2, 2011 Share Posted November 2, 2011 In ValidateForm() you return from the function as soon as the first condition is evaluated, regardless of what happens. I think you intended do something like this pseudocode: if (condition) true: push error into $errors array false: (nothing) if (othercondition) true: push error into $errors array false: (nothing) return $errors array Quote Link to comment https://forums.phpfreaks.com/topic/250264-got-one-item-to-validate-but-not-two/#findComment-1284135 Share on other sites More sharing options...
NETSYNC Posted November 2, 2011 Author Share Posted November 2, 2011 Thanks for the reply. Makes perfect sense. How would I make an errors array and then return it at the end? I know how to make arrays with just data, but how do I do that based on a condition? Quote Link to comment https://forums.phpfreaks.com/topic/250264-got-one-item-to-validate-but-not-two/#findComment-1284136 Share on other sites More sharing options...
xtopolis Posted November 2, 2011 Share Posted November 2, 2011 $errors = array();//empty array function Validate() { if($x = 1) { $errors[] = "X can't equal one"; } if($y = "cat") { $errors[] = "Meow"; } return $errors; } //check if validate is empty/null/whatever and show errors if they exist Quote Link to comment https://forums.phpfreaks.com/topic/250264-got-one-item-to-validate-but-not-two/#findComment-1284137 Share on other sites More sharing options...
NETSYNC Posted November 2, 2011 Author Share Posted November 2, 2011 Ok based on that I did this. Now neither validate and the script errors on submit. The error is: "Fatal error: Call to undefined function validateForm() in /nfs/c06/h02/mnt/94387/xxxxxxx/formtest.php on line 12" <?php // options for drop-down menu $choices = array('-- Choose Your Item','Anniversary Jacket', 'Anniversary T-Shirt'); $sizes = array('-- Choose Your Size','L', 'XL'); if($_SERVER['REQUEST_METHOD'] == 'GET'){ // display form when GET showForm(array()); } else{ // process form if POST $errors = validateForm(); if(count($errors)) showForm($errors); // if errors show again else print 'Form submitted succesfully!'; // no errors } // function generating form function showForm($errors){ global $choices,$sizes; // set defaults $defaults = array(); foreach($choices as $key => $choice){ if(isset($_POST['item']) && ($_POST['item'] == $key)) $defaults['item'][$key] = 'selected'; else $defaults['item'][$choice] = ''; } foreach($sizes as $key => $size){ if(isset($_POST['size']) && ($_POST['size'] == $key)) $defaults['size'][$key] = 'selected'; else $defaults['size'][$size] = ''; } // print form print "<form action='{$_SERVER['SCRIPT_NAME']}' method='post'>"; print "<div>"; print "<select name='item'>"; foreach($choices as $key => $choice){ print "<option value='{$key}' {$defaults['item'][$key]}>{$choice}</option>"; } print "</select>"; showError('item', $errors); print "</div>"; print "<div>"; print "<select name='size'>"; foreach($sizes as $key => $size){ print "<option value='{$key}' {$defaults['size'][$key]}>{$size}</option>"; } print "</select>"; showError('size', $errors); print "</div>"; print "<input type='submit'/>"; print "</form>"; } // display error function showError($type, $errors){ if(isset($errors[$type])) print "<b>{$errors[$type]}</b>"; } // $errors = array();//empty array function Validate() { if(!(isset($_POST['size']) && (array_key_exists($_POST['size'], $choices)) && $_POST['size'] != 0)) { $errors[size] = "Selet Size"; } if(!(isset($_POST['item']) && (array_key_exists($_POST['item'], $choices)) && $_POST['item'] != 0)) { $errors[item] = "Select Item"; } return $errors; } // ?> Thanks for the help! Learning as I go here. Quote Link to comment https://forums.phpfreaks.com/topic/250264-got-one-item-to-validate-but-not-two/#findComment-1284138 Share on other sites More sharing options...
xtopolis Posted November 2, 2011 Share Posted November 2, 2011 You overwrote your own function name: function Validate() { change it back. Quote Link to comment https://forums.phpfreaks.com/topic/250264-got-one-item-to-validate-but-not-two/#findComment-1284139 Share on other sites More sharing options...
NETSYNC Posted November 2, 2011 Author Share Posted November 2, 2011 Oh duh me! It works now! Thank you sir! Quote Link to comment https://forums.phpfreaks.com/topic/250264-got-one-item-to-validate-but-not-two/#findComment-1284140 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.