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,

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.table1.deletethis[].length; i++){
if(document.table1.deletethis.checked)
stop_subscription.php?id="document.form1.deletethis.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
 
You don't need any JavaScript here, surely? The natural behaviour will be for the form to submit only the checked values to the destination page. Why use JS to reinvent the wheel?

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Don't use square brackets in your form element names. This is asking for trouble if you EVER want to process form element values with Javascript. The square brackets for names and ids are not W3C compliant HTML characters. I know it's a common practice for PHP programmers, but it's a poor practice. That's what's causing at least part of your problems with the validation script.

Lee
 
Hi Dan,

I have now taken out the Javascript and am attempting to just submit the form but I am having problems doing this, I just get redirected to my stop_subscriptions page but nothing happens! Here is what I have now:

<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>

I know its now not a Javascript issue any more, but any help would be much appreciated!

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top