Jump to content


Photo

Going through a checkbox array


  • Please log in to reply
3 replies to this topic

#1 pmorrison

pmorrison
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 March 2006 - 10:00 PM

Hi,

I am new to Javascript and am having a bit of difficulty. 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.

I have used the following javascript, I know there is something wrong with
it, but dont know what:

<script type="text/javascript">
function validate() {
for(var i=0; i < document.table11.deletethis[].length; i++){
if(document.table1.deletethis[i].checked)
stop_subscription.php?id="document.form1.deletethis[i].value"
}
}
</script>

Here are other bits of code I think might be relevant:

<?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=button name="Unsubscribe" value="Unsubscribe" onClick="validate()" style="font-size:10px"/><p>
</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/>';
}
?>

I feel bad just pasting a large slab of code here, but I really have no idea
where Im going wrong!

Cheers,

Paul

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,022 posts

Posted 05 March 2006 - 11:59 AM

You don't need the javascript.

The form tag should have "action='stop_subscription.php' " and the checked checkbox values will be posted in the array $_POST['delete_this'].

Store $user in a hidden form field so that too gets posted.

In "stop_subscription.php"

$user = $_POST['user'];
$injIDList = join (',' , $_POST['delete_this'] );

then

DELETE FROM inj_subscription  WHERE member_id = '$user' AND inj_id IN ($injIDList)

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 pmorrison

pmorrison
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 05 March 2006 - 04:31 PM

Ive had a go at changing this around, adding in the form tag, but I dont know if Ive put it in the right place, I feel Im out of my php depth! I Told a friend Id be able to do this for him, thinking it would be easy, but it appears I misjudged it!

<?php
require_once('member_auth.php') ;
include_once('../includes/connect_db.php');
include_once('../includes/functions.php');
include('../header.php');
?>

<?php
//If user has applied for a subsciption, run this code
// If all is OK, then make a subscription
if ($_REQUEST['stop_subscription']==1) {

if (count($_REQUEST['deletethis']) > 0) {
// Split the subscriptions array so we can get the checkbox values.
foreach ($_POST['deletethis'] as $sub=>$value) {
// stop_subscription.php?id="$"
}

}
}
?>

<div id="title1" align="center">
<h3>Unsubscribe</h3>

<?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>
<form method="post" action="stop_subscription.php">
<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"/>
</form>
</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/>';
}
?>
</div>
<?php
include('../footer.php');
?>

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,022 posts

Posted 05 March 2006 - 09:04 PM

All form elements, such as checkboxes and submit button need to be between <form>..</form> tags
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users