Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello forum members,

 

I am struggling with the validation for the following select menus.

 

Each store has 2 select menus, one for number of computers and one for number of employees.

 

A user must select at least one store, and both select menus of that particular store need to have values greater than 0 before the form can be submitted. If more than one store is selected, then again, both select menus of those stores need to have values greater than 0.

 

 

How do I begin with this validation?

 

 

Please help me. Thank you.

<select class="computers" name="computers[store1]"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>

<select class="employees" name="employees[store1]"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>


<select class="computers" name="computers[store2]"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>

<select class="employees" name="employees[store2]"><option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select>

Share this post


Link to post
Share on other sites
Assuming you have already confirmed that the form was properly submitted:

 



$max_val = 4;


$store1_computers = (int) $_POST['computers']['store1'];
$store1_employees = (int) $_POST['employees']['store1'];
$store2_computers = (int) $_POST['computers']['store2'];
$store2_employees = (int) $_POST['employees']['store2'];


if ($store1_computers > 0 &&
    $store1_computers <= $max_val &&
    $store1_employees > 0 &&
    $store1_employees <= $max_val
    ||
    $store2_computers > 0 &&
    $store2_computers <= $max_val &&
    $store2_employees > 0 &&
    $store2_employees <= $max_val) {
  
  // Validate data submitted.
} else {
  // Something weird happened.
}


 

Of course, you may want to further break things up, but that's the basic gist.

  • Upvote 2

Share this post


Link to post
Share on other sites

Thanks a lot HartleySan!

 

 

Do you think it will be difficult to adapt this validation for data coming from a database? It occurred to me that I won't know beforehand which stores will be displayed or how many values each select menu might have.

Share this post


Link to post
Share on other sites

You should be able to generalize Jon's solution and make it viable for any number of values. Instead of setting explicit keys in the last array, you can use <select name="computers[]"> and <select name="stores[]"> in your HTML. You can then foreach () upon the $_POST['computers'] and stores array to loop trough all values.

  • Upvote 2

Share this post


Link to post
Share on other sites

Hi Antonio, thank you very much for your assistance. I will try that. I am using the name of the stores as keys, hopefully it will still work.

 

I hope you and HartleySan are both well.

 

Cheers

 <select name="employees['.$row['store_name'].']">';
 <select name="computers['.$row['store_name'].']">';

Share this post


Link to post
Share on other sites

Morning guys,

 

I am sorry but I haven't got the foggiest idea how to integrate the foreach loops with the code that HartleySan provided. And I've been at it for a few hours.

 

Could you please provide me with an example of how to do this?

 

I don't know if this is relevant but the receiving page needs to receive the values in such a manner so that I can access both the keys and the values for each store. This is because I'm printing out each store name (which is the key) and underneath the values of the two select menus.  The foreach loop I've used just sent the values to the receiving page so I couldn't access the key.

 

 

Thank you again.

Share this post


Link to post
Share on other sites

You should be able to get them by a foreach.

foreach ( $_POST['employees'] as $key => $store )
{
   echo "{$key}: {$store} <br>";
}

The same principle applies for computers. The one thing you might have to do is to assign the store key instead of the name, but I had a little problem understanding what you really try to do here. Hope this helps out.

Share this post


Link to post
Share on other sites

Hi Antonio,

 

thank you for getting back to me.

 

I know how use foreach loops but don't know how to use the loops to check that all $_POST['employees'] and all $_POST['computers'] have values bigger than 0 before the form can be submitted. It is the combining of the foreach loops and the checking of values bigger than 0 that is giving me trouble.

 

All of the stores come from a database. I might have a page with 10 stores, each store having an employee and a computers select menu. A website visitor can select one or more stores, and for each store selected both select menus must have values bigger than 0 before form submission can occur. And for each store's select menus I've included the store's name in the name attribute, so that I can access the store's name on the receiving page.

 

 

Sorry to trouble you with this, but maybe someone can help me figure this out.

 

 

I appreciate your help!

Share this post


Link to post
Share on other sites

if ( validateEmployees ( $_POST['employees'] )
{
   echo "All values are Integers greater than zero.";
}
else 
{
   echo "Invalid data";
} 

function validateEmployees( array $employees )
{
    if ( empty($eployees) ) { return false; }
    $filtered = array_filter($employees, 'isPositiveInteger');
    return $count($employees) === $count($filtered);
}

function isPositiveInteger($value)
{
    return is_int((int)$value) and 0 < intval($value, 10);
}

Something like this could work. Play around with something similar until you get it right. Hope it helps.

Share this post


Link to post
Share on other sites
All right. Let's say you have a data model you got from the DB like the following:

 



$stores = array(
  1 => array(
    'computers' => 12,
    'employees' => 7
  ),
  2 => array(
    'computers' => 24,
    'employees' => 11
  ),
  3 => array(
    'computers' => 5,
    'employees' => 2
  ),
  4 => array(
    'computers' => 58,
    'employees' => 20
  )
);


 

Then you'd probably create the HTML as follows:

 



foreach ($stores as $store_num => $store) {
  echo '<select name="stores[1][computers]" class="computers">';
  
  for ($i = 0, $len = $store['computers']; $i <= $len; $i++) {
    echo '<option value="' . $i . '">' . $i . '</option>';
  }
  
  echo '</select>';
  echo '<select name="stores[1][employees]" class="employees">';
  
  for ($i = 0, $len = $store['employees']; $i <= $len; $i++) {
    echo '<option value="' . $i . '">' . $i . '</option>';
  }
  
  echo '</select>';
}


 

By setting things up this way, you will have a $_POST array that matches the structure of your original data model. If you post back to the original script, then you will already have the total number of computers/employees for each store from your DB call (i.e., your $stores model at the top of this post), but if you post to a different script, then you will need to create hidden inputs so that you can post those max values for each store to the form-processing script so you can evaluate the validity of the selected values.

 

Once you have the $_POST array (assuming you posted to the same script), you'd perform validation like the following:

 



foreach ($_POST['stores'] as $store_num => $store) {
  $computers = (int) $store['computers'];
  $employees = (int) $store['employees'];
  
  if ($computers > 0 &&
      $computers <= $stores[$store_num]['computers'] &&
      $employees > 0 &&
      $employees <= $stores[$store_num]['employees']) {
    
    // Valid number of computers and employees was selected for that particular store.
  } else {
    // An invalid value for either computers or employees (or both) was posted, or 0 was posted for one or both values.
  }
}


 

Obviously, once you have verified whether the posted data is valid or not, you can then decide what to do from there in terms of processing the data and/or showing error messages.

 

That all make sense?

  • Upvote 2

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...