Jump to content

Got one item to validate, but not two


NETSYNC

Recommended Posts

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;

}







?>

Link to comment
https://forums.phpfreaks.com/topic/250264-got-one-item-to-validate-but-not-two/
Share on other sites

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

$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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.