Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Going through a checkbox array

Status
Not open for further replies.

pmorrison1985

Programmer
Mar 4, 2006
7
GB
Hi,

On my site, in order for a member to unsubscribe from an article, they go to the'Unsubscribe' page where they get a table of all of the articles that they are subscribed to. There is a table with each article, and a checkbox next to each article. For each checked checkbox it should send the value of the checkbox to stop_subscription.php.

At the moment, when I click on the submit button I just get redirected to my stop_subscriptions page but nothing happens! Here is my code:

<form method="post" action="stop_subscription.php">
<?php
// Make table if member has specific injustice subscriptions
$sql = "SELECT i.type, i.title , s.inj_id FROM inj_subscription s, injustices i WHERE s.member_id = '$user' AND i.inj_id = s.inj_id AND s.inj_id IS NOT null" ;
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result) ;
if ($row) {
echo "<div><span>Specific Injustice Subsriptions:</span><br/>" ;
$table_start ='<table width="90%" table name="table2" align="center" cellspacing="0" border="0">
<tr>
<th width="20%">Type</th>
<th width="70%">Title</th>
<th width="10%"></th>
<input type="submit" name="Submit" value="Submit" style="font-size:10px"/><p>
<input type="hidden" name="stop_subscription" value="1"/>
</tr>';
echo $table_start;
$rownum = 0;
while($row) {
$rownum++;
$style = rowcolor($rownum);
$table_rows = "<td id=\"td\" align=\"center\" style=\" $style ;padding:5px\" >" .$row['type'] . "</td>";
$table_rows .= "<td id=\"td\" align=\"center\" style=\" $style ;padding:5px\" >" . $row['title']. "</td> \n\t";
$table_rows .= '<td id="td" align="center" style="'.$style.';padding:5px;" >'."\n".'<input type="checkbox" name="deletethis[]" value="'.$row['inj_id'] . '"></td>'."\n\t";
echo "<tr>" . $table_rows . "</tr>";
$row = mysql_fetch_assoc($result) ;
}
echo '</table><Br/></div><br/>';
}
?>
</form>
 
you have not posted your stop_subscription.php code (which will be relevant in supplying you with an answer).

however, on this page the values of the checkboxes that have been clicked will be in the array $_POST['deletethis']
you could examine the values thus:
Code:
echo "<pre>";
print_r($_POST['deletethis'];
echo "</pre>";
 
Hi, here is the code for the stop_subscription.php file
Code:
 <?php
	//article ID and link it to that user
	if( isset($_GET['id']) ){ 
		$id = $_GET['id'];
		$query = "DELETE FROM inj_subscription WHERE member_id='$user' AND inj_id='$id' AND type IS NULL";
		$result = mysql_query($query) or die('There was a problem with the operation.');
		if($result){
			echo '<p style="font-weight:bold">Unsubscription Successful!</p>';
			echo '<p>To resubscribe, simply find the article and subscribe again.</p>';
		}
	}
?>
I will try using those values and see what is contained in them.

Paul
 
Basically, what I want to do is to send the value of each checked checkbox to stop_subscription. The value of the checkbox is set to the id of the article, so it should just send each item in the deletethis array to stop_subscription, but Im not too sure how to do this, and whether I am going about it in the right way!

Paul
 
you're fine. the values are in the array that i posted above.

here is your code back, fixed up somewhat. i'm not sure where you get the $user variable from, though.

Code:
<?
    //article ID and link it to that user
    if( isset($_POST['deleteitems']) ):
	 
        foreach($_POST['deleteitems'] as $val):
			$in .= ",'$val'";
		endforeach;
		$in = ltrim ($in, ",");
		
        $query = "
			DELETE 
			FROM 
				inj_subscription 
			WHERE 
				member_id='$user' 
				AND 
				inj_id IN ($in)
				AND 
				type IS NULL";
        $result = mysql_query($query) or die('There was a problem with the operation.');
        if($result):
            echo '<p style="font-weight:bold">Unsubscription Successful!</p>';
            echo '<p>To resubscribe, simply find the article and subscribe again.</p>';
        endif;
    endif;
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top