Jump to content
Killersites Forums

how to delete rows based on checkbox checked all / none


srinivasa
 Share

Recommended Posts

IMAGINE i have fetched about 8 rows of table data into one webpage and given chekbox to each row, in the same page

 

there are <links to >checkedall/unchekedall followed by select menu 1.delete messages 2.no action should be performed

 

for ex: if i click on checkedall and select the option delete messages it should delete all checked rows(from the main mysql table also)

 

MY PROBLEM IS I DID CODING FOR checkall / uncheckall its working, HOW TO CODE TO DELETE checked rows from mysql table onselecting delete messages from select dropdown.

 

i feel based on primary key of the cheked box i could delete that....HERE I HAVE ATTACHED SNAPSHOT OF MY DESIGN PAGE,THAT WOULD CLEALY EXPLAINS MY PROBLEM......PLZ PLZ DO REPLY

 

please do reply how can i achieve this waiting for your reply,

 

///////HERE IS THE DATABASE

 

-- --------------------------------------------------------

--

-- Table structure for table `products`

--

CREATE TABLE IF NOT EXISTS `products` (

`product_id` int(11) NOT NULL auto_increment,

`product_name` varchar(150) NOT NULL,

`subcat_id` int(11) NOT NULL,

`price` varchar(20) NOT NULL,

`description` text NOT NULL,

`product_image` varchar(200) NOT NULL,

`uploaded_date` varchar(50) NOT NULL,

PRIMARY KEY (`product_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

Link to comment
Share on other sites

I believe you would want to do this with an array of checkboxes. For example, try running this code:

 

<?php

// This is to collect box array value as global_variables is set off in PHP5 by default
if (isset($_POST['box'])) {
$box=$_POST['box'];

while (list ($key,$val) = @each ($box)) {
	echo "$val,";
}
}

echo "<form method=post action=''>";
echo "<table border='0' cellspacing='0' style='border-collapse: collapse' width='100' >
<tr bgcolor='#ffffff'>
<td width='25%'><input type=checkbox name=box[] value='John'></td>
<td width='25%'> John</td>
<td width='25%'><input type=checkbox name=box[] value='Mike'></td>
<td width='25%'> Mike</td>
<td width='25%'><input type=checkbox name=box[] value='Rone'></td>
<td width='25%'> Rone</td>
</tr>
<tr bgcolor='#f1f1f1'>
<td width='25%'><input type=checkbox name=box[] value='Mathew'></td>
<td width='25%'> Mathew</td>
<td width='25%'><input type=checkbox name=box[] value='Reid'></td>
<td width='25%'> Reid</td>
<td width='25%'><input type=checkbox name=box[] value='Simon'></td>
<td width='25%'> Simon</td>
</tr>

<tr><td colspan =6 align=center><input type=submit value=Select></form></td></tr>
</table>"; 

?>

 

I imagine you could do something similar, except that you set the value of the checkbox to the id of the row in the database that you want to delete.

 

(Example from http://www.plus2net.com/php_tutorial/array_checkbox.php)

Link to comment
Share on other sites

Here's another example I wrote a while ago that shows a slightly different way to get the values from an array of checkboxes:

 

<?php

// when the form is submitted...
if (isset($_POST["submit"]))
{
if (isset($_POST["place"]))
{
	$place = $_POST["place"];
	if (isset($place[0])) { echo $place[0] . "<br/>"; }
	if (isset($place[1])) { echo $place[1] . "<br/>"; }
	if (isset($place[2])) { echo $place[2] . "<br/>"; }

	/* alternately:
	foreach ($place as $p) {
		echo $p."<br />";
	} */
}
else
{
	echo "nothing selected!";
}
}

?> 

<form action="" method="post">
Please choose type of residence::<br />
Los Angeles:<input type="checkbox" value="la" name="place[]">:<br />
New York:<input type="checkbox" value="ny" name="place[]">:<br />
Sacramento:<input type="checkbox" value="sac" name="place[]">:<br />
<input type="submit" name="submit">
</form>

Link to comment
Share on other sites

your previous reply was given to static data checkbox, actually i had asked for dynamic data where rows are from mysql table.....

 

HERE I HAVE ATTACHED MY WEBPAGE TO SHOW CLEAR SCENARIO ABOUT MY PROBLEM, IN THIS MY PROBLEM IS NOT ABLE TO DELETE ROWS(FROM DATABASE)

 

EXPECTING FAVORABLE REPLY AS SOON AS POSSIBLE.............

sample.htm

Link to comment
Share on other sites

Please look back at the previous topic you posted -- the code I posted has the start of a solution.

 

You said: "i have fetched about 8 rows of table data into one webpage and given chekbox to each row, in the same page"

When you are displaying those rows of data and creating the checkboxes for each row, you'll want to set the value of the checkbox to the id of the row from the database. If you place those checkboxes in an array like I showed you in the code sample, you can use PHP to get that array of checkboxes, and the values the array holds are the ids for each of the rows you need to delete.

Link to comment
Share on other sites

//here is my code parts

 

<script type = "text/javascript">

 

//check all uncheck all

function SetAllCheckBoxes(FormName, FieldName, CheckValue)

{

var ins = document.getElementsByName('mycheckbox[]');

if(CheckValue==false){

for(i=0;i<ins.length;i++)

ins.checked=false;

}

else if(CheckValue==true){

for(i=0;i<ins.length;i++)

ins.checked=true;

}

}

 

//delete all or not

function dropdownmenu(select,FormName,FieldName)

{

var ins = document.getElementsByName('mycheckbox[]');

var i;

var j=0;

var vals= new Array();

var sentdivvals= new Array();

for(i=0;i<ins.length;i++)

{

if(ins.checked==true)

{

vals[j]=ins.value;

sentdivvals[j]=divarr.value;

j++;

}

 

}

 

 

if((j=='0')&&(select.value!=''))

{

alert("Please select message(s) using checkbox(es)");

document.myForm.actionDropdown.value='';

return false;

}

else

{

<?php

@mysql_query("DELETE FROM products WHERE product_id = ");//product id = ?????

?>

}

}

</script>

 

 

//then form in between some code

<input type = checkbox name = 'mycheckbox[]' value = <?php echo $row['product_id'] ?>

//then some code

<a href=\"javascript:void(0);\" onclick=\"SetAllCheckBoxes('myForm', 'myCheckbox', true);\">Check All</a> /

<a href=\"javascript:void(0);\" onclick=\"SetAllCheckBoxes('myForm', 'myCheckbox', false);\">Uncheck All</a></td>

 

<td width = 15% style=\"font-family:verdana;color:gray;font-size:.8pc;\">

<select name=\"actionDropdown\" onchange=\"javascript:dropdownmenu(this,'myForm','myCheckbox');\">

<option value=\"\">With selected:</option>

<option value=\"deletemsgs\">Delete</option>

</select>

//above select part am not getting

 

checked all / unchecked perfectly working but "select part not" plz help me plz ..................here am attaching complete code and snapshot of my page

sample.htm

product-display.php

Link to comment
Share on other sites

This is how I normally do this.. maybe not the best way, but it works for me.

 

 

<input type="checkbox" name="delete" value="1"> delete 1<br>
<input type="checkbox" name="delete" value="2"> delete 2<br>
<input type="checkbox" name="delete" value="3"> delete 3

 

<?
foreach ($_POST['delete'] AS $var=>$val)
{
//do value checking...
//then..
 mysql_query("DELETE FROM `table` WHERE id='$val'");  
}
?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...