Going through a checkbox array

P

Paul Morrison

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

Jim

Paul,
Just an idea here:
gather all the checkbox values, populate them in bulk to an array, then
send those values to your php processing page. Once at your php page,
use php string functions to separate the values and iterate through
your database script etc.
function validate() {
// when called, this function will return the string
"deletionvalues"
// which holds all the values of the user-checked form elements.


//create array to hold values of checked 'delete_this' items:
var deletions = new Array();
for(var i=0; i < document.table11.deletethis[].length; i++){
if(document.table1.deletethis.checked){
// populate array with checked values:
deletions = document.table1.deletethis.value;
}
}
//change array to a string for sending via form:
var deletionvalues = deletions.toString();
return deletionvalues;
}
 
R

RobG

Paul said:
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:

Your script seriously broken, just use a plain form - there is no
requirement for JavaScript at all.

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

Don't post server code, this isn't a PHP forum. Post the code received
at the client (use view source), we'll help you with that. How you
generate it (PHP, perl, JSP, ASP) is up to you.

Anyhow, what is the point of the script? It seems to be using script to
generate an action property that will be generated anyway as a default
of the form without any scripting at all.

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

Use a plain form, e.g.:

<form action="stop_subscription.php">
<label for="cb1">Subscript to mag 1
<input type="checkbox" id="cb1" value="1"></label><br>
<label for="cb2">Subscript to mag 2
<input type="checkbox" id="cb2" value="2"></label><br>
<label for="cb3">Subscript to mag 3
<input type="checkbox" id="cb3" value="3"></label><br>
<input type="submit"><br><input type="reset">
</form>


Where the values 1, 2, 3 etc. are filled-in by your server before
sending the form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top