0

I have a problem. I want to get checked values from checkboxes and add them to a php array but i have no clue on how to do it. I can do it in javascript but i want a php array and i don't know how to convert js array to php array. I am gonna use the array in next php file and search rows from database with those values. I would appreciate if anybody suggests something on how to do it as i have no clue.

while ($row = mysqli_fetch_array($search)) {
            $category = $row["Category"];
            echo<inputtype=\"checkbox\"name=\"category\"value=\"$category\"required/>$kategoria</input><br /><br />";
}

<a id='answer' href='questions.php'><button>Answer to questions!</button></a>

This is how i make the checkboxes and then on button click i move to the next file

3
  • PHP and JavaScript on the web page have separate non-overlapping life spans. It get a JS array to PHP you need to do an ajax call back to the server Commented Apr 12, 2021 at 14:26
  • first of all your code have some mistakes. i hope those were from copy/paste. you just have to put your checkboxes into a html form then post your form to php file. then in php u have have to get values from those checkboxes like $_POST['category']. if they are not checked then they wont be posted so you have check $_POST array if they exist first. Commented Apr 12, 2021 at 14:33
  • Are you looking to do a regular submission or an AJAX request? Commented Apr 12, 2021 at 14:49

2 Answers 2

0

add attribute action=questions.php, method="post" to form instead of using < a href="questions.php"> and place in form and use extract($$__REQUEST) at very first line of questions.php use print_r(extract($_REQUEST)) to print extracted data from form to associative array.

Sign up to request clarification or add additional context in comments.

Comments

0

Use this for your form:

<form method="POST" action="questions.php">
    <input type="checkbox" name="myInput" id="myInput" value="checked" />
    <label for="myInput">Checkbox</label>

    <!-- A submit button -->
    <input type="submit" value="Submit values" />
</form>

Then in questions.php, all your values are added to the variable $_POST:

// Foreach item in the POST array.
foreach ($_POST as $item => $value) {

    // The value will be stored in $value
}

If the checkbox is checked, the $value variable will be checked. Otherwise it will be empty.

Source: PHP: Foreach in a multidimensional array

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.