0

How would I pass an array of multiple checkboxes and values through php, saving to a sql database, as well as being able to pull up the saved data back on the client side?

For example, I have an array that saves checkbox values in the way of '"checked"""checked""""', but not only do I want to save the checked value, I also want to save the form data, as follows :

  • Apples : Checked
  • Oranges : Not Checked
  • Bananas : Checked
  • Tomatoes : Checked

Any help would be GREATLY appreciated, please answer with context of how to do, not just code - still learning!

Here is the code THAT WORKS for this question

processing code :

$salesman = $data['data-invoice-salesman']; // this is an array
                $salesman_array = array(); // create new array
                $salesman_names = array(1 => "User1",2 => "User2",3 => "User3",4 => "User4",5 => "User");
                for($i = 1; $i <= 5; $i++){ // loop from 1 to 5
                if(in_array($i, $salesman)){ // if value exists (has been selected), stack 'checked', if not, stack ''.
                    $salesman_array[$salesman_names[$i]] = "checked";
                } else {
                    $salesman_array[$salesman_names[$i]] = "";
                }
                }
                $salesman_json = mysqli_real_escape_string($this->_con, json_encode($salesman_array)); // encode the array into JSON and then escape it.

form code THAT WORKS :

<?php
        $salesmand = json_decode($invoice['Invoice']['salesman'], true);
        $salesman_names = array(1 => "User1",2 => "User2",3 => "User3",4 => "User4",5 => "User5");
            foreach ($salesman_names AS $i => $name) {
                if ($salesman[$name] == "checked") {
                    echo '<input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" checked/> '.$name.'<br>';
                } else {
                    echo '<input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" /> '.$name.'<br>';
                }
            }
        ?>
            </div>  
7
  • 1
    $data['data-invoice-salesman'] should be $_POST['data-invoice-salesman']. Other than that, it looks like it should work. Although storing JSON strings in a database doesn't seem like a great idea. Commented Dec 31, 2015 at 21:34
  • @Barmar It works to save the checkbox value, not the name of each input. Why would storing JSON strings be a bad idea(they are escaped)? Commented Dec 31, 2015 at 21:35
  • 1
    IT's not a security issue, just a general database design problem. You can't easily search for a particular value, for instance. Commented Dec 31, 2015 at 21:37
  • the "LIKE" search function will not sieve the results? Commented Dec 31, 2015 at 21:39
  • 1
    It can, but it's a very poor way to do it. It can't take advantage of an index. Learn about proper database normalization. Commented Dec 31, 2015 at 21:42

1 Answer 1

1

Add an array that maps the indexes to the name:

$salesman_names = array(
    1 => "Joe Smith",
    2 => "Fred Flintston",
    3 => "George Jetson",
    4 => "John Hamm",
    5 => "Alan Smithee"
);

Then in your loop, you can do:

$salesman_array[$salesan_names[$i]] = "checked";

The JSON that's saved in the database might then look like:

{"Joe Smith":"checked", "Fred Flintson": "", "George Jetson","checked", "John Hamm":"", "Alan Smithee":""}

To display the checkboxes, you would refer to the $salesman_array when displaying it:

$salesman = json_decode($invoice['Invoice']['salesman'], true);
foreach ($salesman_names AS $i => $name) {
    if ($salesman[$name] == "checked") {
        echo '<input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" checked/> '.$name.'<br>';
    } else {
        echo '<input type="checkbox" name="data-invoice-salesman[]" value="'.$i.'" /> '.$name.'<br>';
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

would I not need another column in my db called "products"?
I don't really understand your application. This is what you said the names of the checkboxes are, but then your PHP loop calls them salesmen.
Sorry, the fruit example had nothing to do with my code, my actual code will have 5 salesman names, as well as need the capability to save/retrieve the salesman, and the status of the checkbox
do i replace my $i = $k+1; code with the $salesman array? can you include placement of where your code fits in with mine?
Added that to the answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.