gmail
gmail

Reputation: 1

get checked boxes value by post in php

I'm getting database from database and each row has a id and im showing it like this in html

<td><input type="checkbox" value"1">test1</td></tr>    
<td><input type="checkbox" value"2">test2</td></tr>  

and so on...

now lets say that user checked ten check boxes out of 15 and then clicked submit .
how to get values of those boxes in php???

Upvotes: 0

Views: 5046

Answers (4)

jay.jivani
jay.jivani

Reputation: 1574

Try this way..

<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="1"><label>Test 1</label><br/>
<input type="checkbox" name="check_list[]" value="2"><label>Test 1</label><br/>
<input type="checkbox" name="check_list[]" value="3"><label>Test 3</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}
}
}
?>

Upvotes: 0

Chris Pickett
Chris Pickett

Reputation: 2822

Your checkboxes need to have a name & a value attribute:

<input type='checkbox' name='test1' value='1'> Test1
<input type='checkbox' name='test2' value='1'> Test2

then when that is posted you can access the values in PHP via $_POST:

$test1 = $_POST['test1']
$test2 = $_POST['test2']

Keep in mind that the values will only be returned if the box is checked, so most likely instead of the above PHP, you're more than likely just going to want to check if the value exists.

Upvotes: 2

user557846
user557846

Reputation:

they will be in either the $_GET or the $_POST array

Upvotes: 0

Naftali
Naftali

Reputation: 146360

give the checkboxes names:

<td><input type="checkbox" value"2" name='test'>test2</td></tr>  

Than in php just read the request

$test = $_REQUEST['test']

if the OP doesn't have these checkboxes inside of a , any amount of PHP code will make absolutely no difference (FROM Blender)

Upvotes: 0

Related Questions