Getting ID's from checkboxes - php

I'm trying to get the ID's from a load of checkboxes that are in rows that are printed out on a page using a while statement. Each row from the database has a checkbox next to it with the ID in the checkbox value.
Basically I want to do a update query on the checkbox-selected rows, using the ID.
The code for the checkboxes that I have used is:
<input type="checkbox" name="check_list[]" value="<? echo $rows['id']; ?>">
Then when the code for the submit is:
<?
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $id){
echo "$id was checked! ";
}
}
?>
Just wanted to echo out the results to test that it works before putting it into a query. Trouble is...nothing happens. I just get a blank screen. No error or anything. Surely it should work, it looks right but I don't understand why it doesnt work.
Any help is most appreciated! :)

Tested below code with one test.php file
<?php
if(!empty($_POST['check_list']))
{
foreach($_POST['check_list'] as $id){
echo "<br>$id was checked! ";
}
}
?>
<form method="post" name="frm">
<input type="checkbox" name="check_list[]" value="1"> 1
<input type="checkbox" name="check_list[]" value="2"> 2
<input type="checkbox" name="check_list[]" value="3"> 3
<input type="checkbox" name="check_list[]" value="4"> 4
<input type="submit" name="submit" />
</form>
please check if you are getting $rows['id'] properly. Things should work fine otherwise.
Thanks.

Related

PHP - Handling HTML checkbox array [duplicate]

I have 1 form in with multiple checkboxes in it (each with the code):
<input type="checkbox" name="check_list" value="<? echo $row['Report ID'] ?>">
Where $row['Report ID'] is a primary key in a database -so each value is different.
How would I be able to tell which checkboxes have been checked? (Maybe multiple)
This is for an inbox system and I have a button below that I want (when clicked) to delete all messages (ids of: $row['Report ID']) which have the checkbox's checked.
Set the name in the form to check_list[] and you will be able to access all the checkboxes as an array($_POST['check_list'][]).
Here's a little sample as requested:
<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="value 1">
<input type="checkbox" name="check_list[]" value="value 2">
<input type="checkbox" name="check_list[]" value="value 3">
<input type="checkbox" name="check_list[]" value="value 4">
<input type="checkbox" name="check_list[]" value="value 5">
<input type="submit" />
</form>
<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
}
}
?>
Edit To reflect what #Marc said in the comment below.
You can do a loop through all the posted values.
HTML:
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
<input type="checkbox" name="check_list[]" value="<?=$rowid?>" />
PHP:
foreach($_POST['check_list'] as $item){
// query to delete where item = $item
}
you have to name your checkboxes accordingly:
<input type="checkbox" name="check_list[]" value="…" />
you can then access all checked checkboxes with
// loop over checked checkboxes
foreach($_POST['check_list'] as $checkbox) {
// do something
}
ps. make sure to properly escape your output (htmlspecialchars())
<input type="checkbox" name="check_list[<? echo $row['Report ID'] ?>]" value="<? echo $row['Report ID'] ?>">
And after the post, you can loop through them:
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $report_id){
echo "$report_id was checked! ";
}
}
Or get a certain value posted from previous page:
if(isset($_POST['check_list'][$report_id])){
echo $report_id . " was checked!<br/>";
}
It's pretty simple. Pay attention and you'll get it right away! :)
You will create a html array, which will be then sent to php array.
Your html code will look like this:
<input type="checkbox" name="check_list[1]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[2]" alt="Checkbox" value="checked">
<input type="checkbox" name="check_list[3]" alt="Checkbox" value="checked">
Where [1] [2] [3] are the IDs of your messages, meaning that you will echo your $row['Report ID'] in their place.
Then, when you submit the form, your PHP array will look like this:
print_r($check_list)
[1] => checked
[3] => checked
Depending on which were checked and which were not.
I'm sure you can continue from this point forward.

Fetch a checkbox list and get them in mysql

I'm fetching values from a checkbox list and would like to get them in mysql, just like the following sql query shows:
<form action="index2.php" method="get">
<input type="checkbox" name="check_list[]" alt="Checkbox" value="one">One
<input type="checkbox" name="check_list[]" alt="Checkbox" value="two">Two
<input type="checkbox" name="check_list[]" alt="Checkbox" value="three">Three
<input type="submit" value="Submit">
<?php
if(!empty($_GET['check_list']))
{
foreach($_GET['check_list'] as $check)
{
$ids = implode(',', $check);
echo $ids;
}
}
$sql="SELECT * FROM table WHERE activity IN($ids)";
I've tried with array(), implode(), explode()...
Nothing works.
this is a simplified code: I did not put the code against injection.
Can anybody be of help. Thanks :)
?>
Can anybody be of help? Thanks very much in advance :)

Default check a checkbox html

I am trying to get a checkbox checked by default, but everything I have tried doesn't seem to work. I don't know if it has to do with the PHP that is in the code.
function show_subscription_checkbox ($id='0') {
global $sg_subscribe;
sg_subscribe_start();
if ( $sg_subscribe->checkbox_shown ) return $id;
if ( !$email = $sg_subscribe->current_viewer_subscription_status() ) :
$checked_status = ( !empty($_COOKIE['subscribe_checkbox_'.COOKIEHASH]) && 'checked' == $_COOKIE['subscribe_checkbox_'.COOKIEHASH] ) ? true : false;
?>
<p <?php if ($sg_subscribe->clear_both) echo 'style="clear: both;" '; ?>class="subscribe-to-comments">
<input type="checkbox" name="subscribe" id="subscribe" value="subscribe" style="width: auto;" <?php if ( $checked_status ) echo 'checked="checked" '; ?>/>
<label for="subscribe"><?php echo $sg_subscribe->not_subscribed_text; ?></label>
</p>
This is a wordpress plugin that allows you to subscribe to blog comments.
I have tried
echo 'checked=\"checked\" ';
echo 'checked="checked" ' ;
echo 'checked> ';
The plugin author states that you used to be able to default check the checkbox but not anymore.
Since this is showing up in google for "default checked checkbox", I figured I'd answer it. Alpay was right: The correct way to ensure that a checkbox is checked by default is like so (followed by an example of one that is not checked):
<input type="checkbox" name="vehicle" value="Car" checked> I have a car
<input type="checkbox" name="vehicle" value="Bike"> I have a bike
Answer was found on w3schools. The author of the original question was having trouble with his PHP code, which is not at all related to the question title.
In HTML, if you want a checkbox to be checked by default, see the following;
<input type="checkbox" name="name1" value="uc"> This checkbox is unchecked <br>
<input type="checkbox" name="name2" value="c" checked> This checkbox is checked<br>
So, you might consider changing
<?php if ( $checked_status ) echo 'checked="checked" '; ?>
to
<?php if ( $checked_status ) echo 'checked'; ?>
The problem is not in the HTML markup being generated; echo 'checked="checked" ', as in the question, works well, and so would the simpler echo 'checked'.
The problem is with the condition $checked_status. You are testing for a variable that is undefined, as far as the code posted is considered.
I had the same problem. I figured out that I was trying to put the checkbox into a table but left out the and.
didn't check:
if(!$stump){echo '<input type="checkbox" name="stump" value="stump" checked="checked"><b> Stump Job!</b>';}
checked:
if(!$stump){echo '<td><input type="checkbox" name="stump" value="stump" checked="checked"><b> Stump Job!</b></td>';}
In regular PHP you can use this to "save" the checked state after its been submitted.
<form name="checkbox" method="post" action="#" >
<input type="checkbox" name="checkbox1" value="Bike" <?php if ($_POST['checkbox1']=="Bike") echo "checked";?>>I have a bike
<input type="checkbox" name="checkbox2" value="Car" <?php if ($_POST['checkbox2']=="Car") echo "checked";?>>I have a car
<input type="submit" name="submit" value="Display this Data" />
if you want to use this data for something else after the submit, just add:
<?php
if(isset($_POST['checkbox1']) OR isset($_POST['checkbox2']) )
{
echo "for 1 : ".$_POST['checkbox1']."for 2: ".$_POST['checkbox2'];
}
?>
if you want to clear the form (basically clear all the post data) you can add:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="submit" name="Clear all form data" value= "Clear this form">
</form>

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???
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.
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)
they will be in either the $_GET or the $_POST array
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>";
}
}
}
?>

Checkbox checked with PHP form post?

How do I check a checkbox?
I've tried 1, On, and Yes. That doesn't work. Putting the worked "checked" alone works, but then how do I check with PHP after a form post of the checkbox is checked?
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
A checkbox will only be a successful control if it is checked.
Controls that are not successful are not submitted as data.
Therefore, you can tell if a checkbox is checked by seeing if its value has been submitted.
E.g.
if ($_POST['chk']['newmsg2'] == 1) {
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" <?php if ($_POST['chk']['newmsg2']): ?>checked="checked"<?php endif; ?> />
Here is the code;
<form action="test.php" method="POST">
<input type="checkbox" class="inputcheckbox" id="newmsg" name=chk[newmsg2] value="1" />
<input type="submit">
</form>
<?php
$check = $_POST['chk']['newmsg2'];
echo "***$check****"
?>
If it is checked $check will show 1.

Categories