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
https://forums.phpfreaks.com/topic/210573-php-and-checkboxes/#findComment-1098592
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
https://forums.phpfreaks.com/topic/210573-php-and-checkboxes/#findComment-1098609
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
https://forums.phpfreaks.com/topic/210573-php-and-checkboxes/#findComment-1098612
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
https://forums.phpfreaks.com/topic/210573-php-and-checkboxes/#findComment-1098642
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.