Jump to content

Recommended Posts

Something like this should work

 


<form name="my_form" method="POST" action="action.php">

<table align="center" border="0">

<?php

$query = mysql_query("SELECT * FROM forumdb");
while($r = mysql_fetch_array($query)){
echo "<tr><td>";
echo $r['posts']." </td><td><input type='checkbox' name='select[]' value='".$r['id']."'></td></tr>"; 

//just print a title and a text box. note the [], this creates an array of all selected checkboxes.

}

?>

<tr>
<td><input type="submit" name="submit" value="Delete Selected Posts"></td>
</tr>
</table>

 

and on your  'action.php' page....

 


<?php

$selected = implode(",", $_POST['select']); //this would make the array into something like this 1,3,7,8 etc...

if($selected == ''){

echo "Nothing selected!";

}else{

mysql_query("DELETE FROM forumdb WHERE id in ($selected)"); //deletes all from table if id is in variable

echo "Deleted posts!";

}

?>

 

<input type='checkbox' name='select[]'

 

I've worked with html for > 10 years, and this taught me something new :-)

I've named other form elements with array notation before (select multiples etc.), but until now I've created serialized strings of dynamically created checkbox names and sent the string data with a hidden box :-) Imagined that I tried this a long time ago and found that it wasn't possible with checkboxes, but obviously I either tried it a bit too early, or i did something wrong :-)

 

Just shows why programming/markup is so fun; you learn something new every day, even when you don't expect it :-)

okay then you might also enjoy this bit of advice: if you have a list of checkboxes and you do that, it will post the ones you checked, but the array will always start at 0 and count from there, so even if you check the 1st, 5th, 6th, and 8th checkbox out of 10 (for example), the array will be posted as $select[0]-$select[3].  The point is, you don't know which checkboxes were checked.  To get around that, you can explicitly assign the array position in the form, like name = 'select[0]', name = 'select[1]' etc... that way it will post the $select array, but the info will be posted in the specific element positions, so you can know which ones were checked. 

okay then you might also enjoy this bit of advice: if you have a list of checkboxes and you do that, it will post the ones you checked, but the array will always start at 0 and count from there, so even if you check the 1st, 5th, 6th, and 8th checkbox out of 10 (for example), the array will be posted as $select[0]-$select[3].  The point is, you don't know which checkboxes were checked.  To get around that, you can explicitly assign the array position in the form, like name = 'select[0]', name = 'select[1]' etc... that way it will post the $select array, but the info will be posted in the specific element positions, so you can know which ones were checked. 

 

Well, thats evidently :-)

But when you supply the value for the checkbox like in the example above, you got all that you need. The array order is not interesting, only the values inside it.

you mean you can't think of a single reason that that wouldn't be useful?

 

Sorry crayon, my reply was unclear and might lead to this misunderstanding.

Yes, I can think of several ways it can be helpful, a "hellofalot" actually. In most cases i would too say that this is a better solution than just using []. I Just ment to say that in the context of this thread, the order of the array was not interesting. I had actually tested numbered indexing [\d] 30 seconds after i read about [] in this thread :-)

 

In a database-listing context, the possibility of using row id's as array indexes for the checkbox array creates lots of opportunities. A checkbox object now has posting now has 3 interesting variables; name, value, unchecked. I see new possibilites in both posting and js event handling.

 

Anyways, I do appreachiate you trying to give me some good advice :-)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.