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.
Related
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
}
}
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 */
}
Okay so I have a script which is basically like an order form. However my only input options are radio buttons. When I run my script, I noticed that it turned back errors. Instead I would like it to display and empty field. The page posts back to itself and it updates a box I left blank on the bottom before submission. After submission details are filled in from a multitude. I also used a two dimensional array if that makes any difference. So my problems are with empty variables in the forms. I was not able to fix this and I think it might be the way I'm using it. I get an undefined index error for the variables that receive the data. In this case $settype and $differentype come out as errors since they are defined to be whatever was in the textbox. I thought that by using empty() I could eliminate that issue. I did not approach it correctly of course. Can anyone give me some guidance here? Thank you.
<form name="form1" method="POST" action="order.php">
<input type="radio" name="set" value="1" /> 1
<input type="radio" name="set" value="2" /> 2
<input type="radio" name="set" value="3" /> 3
<input type="radio" name="different" value="a" /> a
<input type="radio" name="different" value="b" />
<input type="radio" name="different" value="c" />
<input type="submit" name="submit1" value="login" />
</form>
<?php
if(isset($_POST['submit'])) {
$settype = $_POST['set'];
$differentype = $_POST['different'];
if ($settype == '1' && $differenttype =='a'){
$order = $field[0][1];
}
if (empty($settype) && empty($differenttype)){
$order = "";
}
}
?>
<table>
<tr>
<td>
<?php print($order); ?>
</td>
</tr>
</table>
Where is your input submit ? like this <input type="submit" name="submit" value="submit" />
EDIT 1: You should have a default radio button selected value like
<input type="radio" name="set" value="1" checked="checked"/> 1
<input type="radio" name="different" value="a" checked="checked" /> a
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...
});
I need some help saving the value of the checkbox that user has selected.
I have two checkboxes in the form:
<form action="process.php" method="POST">
Option One: <input type="checkbox" name="check1" value="1"/>
Option Two: <input type="checkbox" name="check2" value="2"/>
<br>
<input type="submit" value="Submit" />
</form>
<?php
if(isset($_POST['check1'])){
//execute function A
}
elseif(isset($_POST['check2'])){
//execute function B
}
?>
Now if the user ticks "Option One" and clicks submit, the checkbox remains checked and the php code executes some function otherwise if the user ticks "Option two" another function is executed and the checkbox 2 remains checked.
Please someone help me sort this out.
Thankyou!
If you want to keep them checked after submitting:
Option One: <input type="checkbox" name="check1" value="1" <?php if(isset($_POST['check1'])) echo 'checked="checked"'; ?> />
Option Two: <input type="checkbox" name="check2" value="2" <?php if(isset($_POST['check2'])) echo 'checked="checked"'; ?> />