0

I have a php file that is receiving some checkbox values from a form. Both the checkbox name and value are set up to match an Item_Name field in a mysql table. My current attempt is below:

while($row = $items->fetch_assoc()){
        if( isset($_POST[$row['Item_Name']])) {
            \\ Code to perform if true, mostly echoes
        }
    }

//Checkbox setup: 

echo  "<input type='checkbox' name=" . $row['Item_Name'] .  "value=" . $row['Item_Name'] .  ">"

$items is the data returned by my query of the mysql table. Currently none of the echoes inside the if are triggering so I think something is wrong with my if statement, but I'm to new to php to know what is wrong exactly.

8
  • There's not enough code to know why. Let's say $row['Item_Name'] is Bags. Now PHP is looking for $_POST['bags']. Beyond that, it's up to you to debug it. Maybe try var_dump($_POST);? Commented Dec 14, 2019 at 4:44
  • You need a foreach for this and making sure that the inputs have the [] multiple array brackets. Commented Dec 14, 2019 at 4:46
  • @Machavity my bad I did a var dump of $_POST and it returned this array(2) { ["Applevalue=Apple"]=> string(2) "on" ["Milkvalue=Milk"]=> string(2) "on" } Commented Dec 14, 2019 at 4:46
  • @FunkFortyNiner would the foreach be replacing the while loop here. I'm just slightly confused on what it would look like Commented Dec 14, 2019 at 4:50
  • I'm guessing the values in $row['Item_Name'] are actually Apple and Milk etc. so the problem would appear to be in the generation of the name attributes of your checkboxes. Commented Dec 14, 2019 at 4:57

1 Answer 1

1

Your problem is in your checkbox setup; you are missing quotes around the name and value attributes. Try this instead:

echo  "<input type='checkbox' name=\"" . $row['Item_Name'] .  "\" value=\"" . $row['Item_Name'] .  "\">";
Sign up to request clarification or add additional context in comments.

Comments

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.