Multiple actions for checkbox list - php

I have a list of multiple items with having a checkbox for each row (like gmail list of emails).
User selects checkboxes and then press an action button to perform some action on selected items ( delete for example). Then the list of selected items will be sent to php file for background processing.
But what is the solution for performing multiple actions on selected checkboxes ( For instance, in gmail, we do delete, mark as spam, mark as read/unread etc..).
Here is what I have:

Use different "submit buttons" for each actions. Give same name but different values. Then check it to backend
For example,
HTML
<form action="backend.php" method="post" >
<input type="submit" name="action" value="Delete" />
<input type="submit" name="action" value="Move" />
<input type="submit" name="action" value="Copy" />
<input type="checkbox" name="selection[]" value="value 1" >
<input type="checkbox" name="selection[]" value="value 2" >
<input type="checkbox" name="selection[]" value="value 3" >
<input type="checkbox" name="selection[]" value="value 4" >
<input type="checkbox" name="selection[]" value="value 5" >
</form>
PHP
..................
..................
if($_POST['action'] == "Delete")
.......
else if($_POST['action'] == "Move")
.......
else if($_POST['action'] == "Copy")
.......

I would give the checkboxes a name that would result in a php item array after a post the form.
For example, name="item[]"
When the form is posted you can then loop through the items in the array with,
foreach($_POST['item'] as $item) {
/* do action */
}

Related

How to add each of checked checkboxes to a signle row in MySQL

I have several checkboxes that contain names (ids as referrence to database) - see code below. How can I select all checked values and add them to database (via MySQL) each row for each checked?
<input type="checkbox" value="1" name="names[]">John
<input type="checkbox" value="2" name="names[]">Peter
<input type="checkbox" value="3" name="names[]">Mike
<input type="checkbox" value="4" name="names[]">Kattie
<input type="submit" value="Send" name="send">
After clicking "send" with requiered checked names, the result in database should look like this (I selected John and Mike):
Id
1
3
(only selected ones)
How can I achieve that?
You need to wrap your inputs in a <form> element and give this form a method of post:
<form method="post">
<input type="checkbox" value="1" name="names[]" />John
<input type="checkbox" value="2" name="names[]" />Peter
<input type="checkbox" value="3" name="names[]" />Mike
<input type="checkbox" value="4" name="names[]" />Kattie
<input type="submit" value="Send" name="send" />
</form>
This will allow you to post submitted data from your form inputs to your PHP.
Note: If your HTML is in a different file (ie not in the same file as your form) you can add the action attribute to your form (eg: action="fileWithPHP.php")
Now you can access all checked checkboxes in your PHP using $_POST['names']. This will allow you to get your array of checked values. You can then use a foreach loop to loop through every value in your array:
<?php
if(isset($_POST['names'])) {
$names = $_POST['names'];
foreach($names as $name) {
echo "Add " . $name . " to db here<br />"; // add $name to db
}
}
?>
You can wrap the inputs around a <form> and send it to php and retrieve using $_GET or $_POST and update the database.
I have used POST method here.
HTML:
<form action="test.php" method="post">
<input type="checkbox" value="1" name="names[]">John
<input type="checkbox" value="2" name="names[]">Peter
<input type="checkbox" value="3" name="names[]">Mike
<input type="checkbox" value="4" name="names[]">Kattie
<input type="submit" value="Send" name="send">
</form>
PHP:
if(!empty($_POST['names'])) {
foreach($_POST['names'] as $check) {
echo $check; //instead of echo you can insert it to the database
}
}

how-to submit form with multiple groups of radio buttons?

goal: post several groups of radio buttons to array (as a message, to database, etc.)
Hi there,
I was wondering how i can post my selections of several groups of radio buttons.
Here is the code:
<body>
<form action="" method="post">
<select name="module 1">
<input type="radio" name="radioA" value="Radio 1">Radio 1
<input type="radio" name="radioA" value="Radio 2">Radio 2
<input type="radio" name="radioA" value="Radio 3">Radio 3
<input type="submit" name="submit" value="Get Selected Values" />
</select>
<br>
<select name="module 2">
<input type="radio" name="radioB" value="Radio 21">Radio 21
<input type="radio" name="radioB" value="Radio 22">Radio 22
<input type="radio" name="radioB" value="Radio 23">Radio 23
<input type="submit" name="submit" value="Get Selected Values" />
</select>
<br>
<select name="module 3">
<input type="radio" name="radioC" value="Radio 31">Radio 31
<input type="radio" name="radioC" value="Radio 32">Radio 32
<input type="radio" name="radioC" value="Radio 33">Radio 33
<input type="submit" name="submitC" value="Get Selected Values" />
</select>
<br>
</form>
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['radioA']))
{
echo "You have selected :".$_POST['radioA']; // Displaying Selected Value
}
}
?>
</body>
...But I can't make it display the list of my selections, but only one instructions, instead of 3x (one for each).
Long story short... How can I treat this as a 1 global set of 3 groups, having only 1 "submit" button to process all 3 groups at once?
Txs a lot, and have a nice weekend.
You just need to remove the other two submit buttons and only display one. As long as it is inside the <form> it will submit all the radio button "groups" to the $_POST.
Also, remove the select tags as you are using them incorrectly. Only an option or optgroup should be nested inside a select element.
You will need to reference each one individually to see the selected response:
echo $_POST['radioA'];
echo $_POST['radioB'];
echo $_POST['radioC'];

Different checkboxes send the user to different pages when checked

I need a form in which the checkboxes would open different pages based on their selection when the form is submitted.
So, say I have this simple form:
<form action="" method="post">
<input value="1" type="checkbox" name="sign" />
<input value="2" type="checkbox" name="sign" />
<input value="3" type="checkbox" name="sign" />
<input value="4" type="checkbox" name="sign" />
<input value="5" type="checkbox" name="sign" />
<input type="submit" />
</form>
When an user checks value 1 and submits, he will be redirected to page A. If a user checks 1 and 4 he will be redirected to a different page (page F, for instance). If a user checks 2, 3 and 4 he will be redirected to page R, and so on... There would be 25 different combinations, and therefore, 25 different page results for this form when an user submits it.
In other words, when the form is submitted somehow the system would read which checkboxes were checked and associate each possible combination with a different URL.
Can it be done? If so, how? Anyone ever made something similar? I've searched a long time for solutions, but found only slightly similar ones, not exactly what I need, so any help would be appreciated.
HTML:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[1]" />
<input value="2" type="checkbox" name="sign[2]" />
<input value="3" type="checkbox" name="sign[3]" />
<input value="4" type="checkbox" name="sign[4]" />
<input value="5" type="checkbox" name="sign[5]" />
<input type="submit" />
</form>
PHP:
if (isset($_POST['sign'][1]))
header("Location: a.php");
elseif(isset($_POST['sign'][2]) AND isset($_POST['sign'][3]))
header("Location: b.php");
This can be done in a couple of ways. One is that you can use javascript to intercept the form submission, change the value of the form "action", and then execute the submit.
A second approach might be to just send all this data to a single script and based on the selected values perform a redirect to the intended page.
From should look like:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
And the post like:
if(!empty($_POST['sign'])) {
if(in_array(1, $_POST['sign'])) {
// if just 1, go to page
} elseif(in_array(1, $_POST['sign']) && in_array(4, $_POST['sign'])) {
// if 1 and 4, go to page
}
}
You can just consider each of the inputs as a bit. So, you will always have 5 bits to consider.
Then define an associative array of 25 entries corresponding to each of the possible values:
00001
00010
...
var links = {
"00001" : "www.google.com",
];
Then, when you submit the form, just set the target attribute of the form based on the value.
If I were you and I had no choice than following this idea, I would use something like that in jQuery (assuming your post vars are treated in the page you want to reach):
<form action="" method="post" id="myForm">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
$("#myForm").submit(function() {
var checked_array = new Array();
$("#myForm input").each(function() {
if ($(this).is(":checked")
checked_array.push($(this).attr("value"));
});
if ( checked_array.indexOf(2) !== -1 && checked_array.indexOf(5) !== -1)
("#myForm").attr("action", "/url1.php") ;
else if etc...
});

How to allow only 2 selection checkbox in a form with?

I want to put a form on my website for giving away 3 prizes. Each member to be able to select only 2 item on the list that they wish to win.
I have created the form and with JavaScript it works fine, but it's client side and can't be trusted.
What is the best way to implement such thing and be sure that each user can ONLY select 2 item? best way to do it with PHP?
Appreciate any help.
jQuery is boss, have a look here.
http://gravitywiz.com/2012/06/11/limiting-how-many-checkboxes-can-be-checked/
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Price 1<input value="price1" type="checkbox" name="price[]">
Price 2<input value="price2" type="checkbox" value="price[]">
Price 3<input value="price3" type="checkbox" value="price[]">
<input type="submit" name="send" value="Send" />
</form>
Then check the size of the array $_POST['price']:
<?
if(isset($_POST['send'])) {
if(sizeof($_POST['price']) == 2) {
//here you go
} else {
echo "error, pick exactly two prices!";
}
}
?>
Why won't you use radiobuttons?
<form method="post">
Price 1 <input type="radio" value="price 1" />
Price 2 <input type="radio" value="price 2" />
Price 3 <input type="radio" value="price 3" />
</form>
And if you want to allow multiple prizes just switch to checkboxes
<form method="post">
Price 1 <input type="checkbox" name="price[]" value="price 1" />
Price 2 <input type="checkbox" name="price[]" value="price 2" />
Price 3 <input type="checkbox" name="price[]" value="price 3" />
</form>

Delete multilple marked items

How can I delete multiple items from a list displayed in html.
For example
<ul>
<li>item 1</li>checkbox
<li>item 2</li>
</ul>
Delete items
The easy way to do this in PHP is to give all your check boxes the same name, and have that name end with []:
<input type="checkbox" name="item[]" value="1" />
<input type="checkbox" name="item[]" value="2" />
<input type="checkbox" name="item[]" value="3" />
Then your script gets all the checked values as an array:
if(empty($_REQUEST['item'])) {
// No items checked
}
else {
foreach($_REQUEST['item'] as $id) {
// delete the item with the id $id
}
}
To make this work, you need to put the check boxes into a <form>, and use a submit button (not a link -- that will not work, unless you start using JavaScript) like this:
<form method="post" target="some/php/script.php">
<input type="checkbox" name="item[]" value="1" />
<input type="checkbox" name="item[]" value="2" />
<input type="checkbox" name="item[]" value="3" />
<input type="submit" name="delete" value="Delete checked items" />
</form>
There are some possible improvements to this (see Crozin's answer), but you should get the basics down first.
First of all you should use a form. You can use checkboxes to select items to delete and then use following query:
DELETE FROM tbl_name WHERE id_col_name IN (1, 2, 3, 4, 5, 6, 7, 8);
HTML form itself could look like following:
<form ...>
...
<li>
<label>Delete item #123 <input type="checkbox" name="delete[123]" />
</li>
...
</form>
Then list of IDs is easily available under $_POST['delete'] array.

Categories