Jump to content

Recommended Posts

Hello,

     I am trying phpfreaks out without much luck here. If anybody here truly knows how to validate checkboxes as a group please give some assistance. I had to remove the array from the 'name' attribute in my checkbox inputs in order to get the user's checked selections to be properly distributed into separate database columns (not all in one column as a string of text). I am now able to successfully get the entries into the database as "1" for selected and "0" for un-selected. Now however, since there is no array that is holding these input names my validation class method does not work. My validation is suppose to show an error if none of these checkboxes are selected.

      I had to change each input name to its own identity ( fruit_selection_apple, etc... ), then set my validation like (see code below). However, this is NOT OOP PHP. I had to hard code these values into the validation class. I am also now getting a notice, " Notice: Undefined index: fruit_selection ". Does anyone legitimately know their PHP enough to help with this issue?

public function check($source, $items = array()) {
			foreach($items as $item => $rules) {
				foreach($rules as $rule => $rule_value) {
										
					$value = trim($source[$item]);					
					$item = escape($item);

               $checkboxvalue =                 
                        (isset($_POST['fruit_selection_apple'])) ||
                        (isset($_POST['fruit_selection_orange'])) ||
                        (isset($_POST['fruit_selection_banana'])) ||
                        (isset($_POST['fruit_selection_pear'])) ||                        
                        (isset($_POST['fruit_selection_kiwi'])
                    )                              
                    if($rule === 'atleastone' && empty($checkboxvalue)) {
                        $this->addError("{$item} You must select at least one checkbox.");
                    }
Link to comment
https://forums.phpfreaks.com/topic/311523-checkbox-insertvalidation-problem/
Share on other sites

Try running the script below. It will display the posted form input.

If you see what is being posted it should give you a clue about how to process it.

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {   // was form data submitted?
    
    if (isset($_POST['fruit'])) {
        echo "Your form inputs were<br>";
        echo '<pre>$_POST = ', print_r($_POST, 1), '</pre>';
        echo "<hr>";
    }
    else echo "No fruits were selected<hr>";  
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Example</title>
<style type='text/css'>
    label {
        display: inline-block;
        width: 80px;
    }
</style>
</head>
<body>
<form method="POST">
    <fieldset>
    <legend>Select fruits</legend>
        <label for='apple'>Apple</label>
        <input type="checkbox" name="fruit['apple']" id="apple" value="1">
        <br>
        <label for='orange'>Orange</label>
        <input type="checkbox" name="fruit['orange']" id="orange" value="1">
        <br>
        <label for='banana'>Banana</label>
        <input type="checkbox" name="fruit['banana']" id="banana" value="1">
        <br>
        <label for='pear'>Pear</label>
        <input type="checkbox" name="fruit['pear']" id="pear" value="1">
        <br>
        <label for='kiwi'>Kiwi</label>
        <input type="checkbox" name="fruit['kiwi']" id="kiwi" value="1">
        <br>
    </fieldset>
    <br>
    <input type="submit" value="Submit">
</form> 

</body>
</html>

 

7 hours ago, ke-jo said:

Does anyone legitimately know their PHP enough to help with this issue?

Yes.  You're issue isn't that the people here don't know what they are doing, the problem is you keep changing things and understanding how it affects the code.  The code you had in your original post in the original thread was fairly close to correct.  Requinix said what you needed to do to fix it (by saying what you need to do, not just giving you the code to do it) but I guess you didn't understand or something.  Then you changed the code to something else which invalidated that advice and started you down a different path.

So let's just back the f* up and go back to that original code with your checkboxes as a simple array.

<input type="checkbox" name="fruit_selection[]" value="fruit_apple">
<input type="checkbox" name="fruit_selection[]" value="fruit_orange">
<input type="checkbox" name="fruit_selection[]" value="fruit_banana">
<input type="checkbox" name="fruit_selection[]" value="fruit_pear">
<input type="checkbox" name="fruit_selection[]" value="fruit_kiwi">

What this will do is create an array when submitted called $_POST['fruit_selection']  The contents of this array will be the choices that the user selected.  The items that were not selected will not exist in the array.  Your original code seems to indicate you almost understood this as you were checking for $_POST['fruit_selection'][0] and so on.  What you seemingly failed to understand is that element [0] in that array does not equate to the checkbox with "fruit_apple", it equates to whichever is the the first checkbox the user actually checked is (which could be fruit_kiwi for example if they only checked the last one).

So what you want to do to get your 1/0 values it not check of those positions in the array exist, but check if the values of your checkboxes exist within that array.  This is what requinix was telling you to do.  One way to do that is using php's in_array function.  For example.

$isKiwiChecked = in_array('fruit_kiwi', $_POST['fruit_selection']);

Now you should have enough information to figure everything out.  So take a time out and spend some time learning rather than blaming everything on the people attempting to help you being idiots.  Use things like var_dump as demonstrated above to get an idea of what data you have available and what format it's in so you can better learn what to do. 

 

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.