Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make array from checkbox form

My friend and I are making a website that compiles news stories based on your interests. Is there and easy way to take the checkbox data and make an array out of the selected checkboxes? Here is our form

<form action="signup.php" method="POST">
    Name: <input type="text" name="name" /> <br />
    Username: <input type="text" name="username"> <br />
    Password: <input type="password" name="pwd" /> <br />
    Email: <input type="text" name="email" /> <br />

    <p>By filling out this we will be able to find news articles that will interest you</p>        <br />
    Politics<input type="checkbox" name="interest[]" value="Politics" /> <br />
    Entertainment<input type="checkbox" name="interest[]" value="Entertainment" /> <br />
    Tech <input type="checkbox" name="interest[]" value="Tech" /> <br />
    Health<input type="checkbox" name="interest[]" value="Health" /> <br />
    Living<input type="checkbox" name="interest[]" value="Living" /> <br />
    Travel <input type="checkbox" name="interest[]" value="Travel" /> <br />
    World<input type="checkbox" name="interest[]" value="World" /> <br />
    Leisure<input type="checkbox" name="interest[]" value="Leisure" /> <br />
    Finance<input type="checkbox" name="interest[]" value="Finance" /> <br />
    Celebrity Gossip<input type="checkbox" name="interest[]" value="Gossip" /> <br />
    Movies<input type="checkbox" name="interest[]" value="Movies" /> <br />
    Sports<input type="checkbox" name="interest[]" value="Sports" /> <br />

    <input type="submit" value="Submit">
</form>

how would we make a php array using this data?

like image 811
austin Avatar asked Dec 23 '10 08:12

austin


People also ask

How do I create an array of checkboxes?

Inside the GetSelected function, first the HTML Table is referenced and then all the CheckBoxes inside it are referenced. Then a loop is executed over the referenced CheckBoxes and inside the loop the value of the selected (checked) CheckBox is inserted into an Array.

How do I make an array of checkboxes in HTML?

To link several checkboxes together to make them into an array in the PHP $_POST array you need to make all of the checkboxes have the same name, and each name must end in "[]". When both of the checkboxes are filled and the form is submitted this produces the following array.

How do you store a selected checkbox value in an array?

Store all selected checkbox value in array using for loop. Inside this function, create a new blank array ( let arr = [] ). Then, store the selected checkbox into a variable using querySelectorAll() method. The querySelector() method returns the first element that matches the specified selectors.


1 Answers

the HTML markup:

<form method="get">
    <input type="checkbox" name="options[]" value="Politics"/> Politics<br/>
    <input type="checkbox" name="options[]" value="Movies"/> Movies<br/>
    <input type="checkbox" name="options[]" value="World "/> World<br/>
    <input type="submit" value="Go!" />
</form>

and in the php code:

$checked = $_GET['options'];
for($i=0; $i < count($checked); $i++){
    echo "Selected " . $checked[$i] . "<br/>";
}
like image 126
The Scrum Meister Avatar answered Oct 06 '22 07:10

The Scrum Meister