Jump to content

PHP and Checkboxes


lalnfl

Recommended Posts

<form method="post">
box 1 <input type="checkbox" name="box[]" value="1"><br />
box 2 <input type="checkbox" name="box[]" value="2"><br />
box 3 <input type="checkbox" name="box[]" value="3"><br />
<input type="submit" name="submit" value="View Checked Boxes">
</form>
<?php
if(isset($_POST['submit'])){
$box = $_POST['box']; // this will be an array

if(count($box) > 0){
	foreach($box AS $id){
		echo $id . "<br />\n";
	}
}
}
?>

 

You give each checkbox the same name with the brackets on the end of it, so in my case box[]. For each checkbox selected it will add that value to an array. The rest is just grabbing the values in that array.

Link to comment
Share on other sites

<form method="post">
box 1 <input type="checkbox" name="box[]" value="1"><br />
box 2 <input type="checkbox" name="box[]" value="2"><br />
box 3 <input type="checkbox" name="box[]" value="3"><br />
<input type="submit" name="submit" value="View Checked Boxes">
</form>
<?php
if(isset($_POST['submit'])){
$box = $_POST['box']; // this will be an array

if(count($box) > 0){
	foreach($box AS $id){
		echo $id . "<br />\n";
	}
}
}
?>

 

You give each checkbox the same name with the brackets on the end of it, so in my case box[]. For each checkbox selected it will add that value to an array. The rest is just grabbing the values in that array.

 

So what if I have if statements where you have echo $id? How would I format it so that the if statements would process?

Link to comment
Share on other sites

Like if statements to check whether or not the "message ID" exists and belongs to that user?

 

<form method="post">
box 1 <input type="checkbox" name="box[]" value="1"><br />
box 2 <input type="checkbox" name="box[]" value="2"><br />
box 3 <input type="checkbox" name="box[]" value="3"><br />
<input type="submit" name="submit" value="View Checked Boxes">
</form>
<?php
if(isset($_POST['submit'])){
$box = $_POST['box']; // this will be an array

if(count($box) > 0){
	foreach($box AS $id){
		$sql = "SELECT * FROM `messages` WHERE `id`='".mysql_real_escape_string($id)."'";
		$res = mysql_query($sql) or die(mysql_error());
		if(mysql_num_rows($res) > 0){
			// msg exists
			$row = mysql_fetch_assoc($res);
			if($row['uid'] == $_SESSION['uid']){
				// msg belongs to logged in user
				$sql2 = "DELETE FROM `messages` WHERE `id`='".mysql_real_escape_string($id)."'";
				$res2 = mysql_query($sql2) or die(mysql_error());
				// msg deleted
			}else {
				// msg doesn't below to user, nothing is deleted
				// you can put your error msgs in here if you want
			}
		}else {
			// msg doesn't exist, nothing is deleted
			// you can put your error msgs in here if you want
		}
	}
}
}
?>

Link to comment
Share on other sites

That's what mgallforever's solution would do. But I would suggest doing it another way. There's no need for all those loops and tons of queries. It can be simplified:

 

$sql = "DELETE FROM messages WHERE id IN (" . implode(", ", $_POST['box']) . ") AND uid = " . $_SESSION['uid'];

Link to comment
Share on other sites

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.