Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Mar 2008
Posts: 14
Reputation: farahphp is an unknown quantity at this point 
Solved Threads: 0
farahphp's Avatar
farahphp farahphp is offline Offline
Newbie Poster

Checkbox Help

 
0
  #1
Nov 11th, 2008
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. <html>
  3. <head>
  4. <script language=javascript>
  5. function updateopen()
  6. {
  7. var c_value = "";
  8. var cb = document.getres.checkbox;
  9. for (var i=0; i < cb.length ; i++)
  10. {
  11. if (document.getres.checkbox[i].checked)
  12. {
  13. c_value = c_value + document.getres.checkbox[i].value + "\n";
  14. }
  15. }
  16. alert(c_value);
  17. //window.opener.document.getElementbyId(numbe33).value = c_value
  18. //window.close();
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <?
  24. echo '<form name="getres" id="getres">
  25. <p align=center><table border=1 cellpadding=3 cellspacing=0><tr>
  26. <th width=100px valign=bottom nowrap>
  27. <font face=arial size=3><br> Item Number</font></th>
  28. <th colspan=2 widht=200px valign=bottom nowrap><font face=arial size=3><br> Item Name </font></th></tr>';
  29.  
  30. for ($g=0;$g < sizeof($a); $g++)
  31. {
  32. echo '<tr><td align=middle name=number[] id=number nowrap><font size=2>'.$a[$g].'&nbsp;</font></td>';
  33. echo '<td align=middle name=itname[] id=itname nowrap><font size=2>'.$b[$g].'&nbsp;</font></td>';
  34. echo '<td><input type=checkbox name=checkbox[] value='.$a[$g].'></td></tr>';
  35. }
  36. echo '</table><br>';
  37. echo '<input type=button value=go onClick="updateopen()"></p>';
  38.  
  39. echo '</form></body></html>';
  40. }
  41. include('Spec_dbclose.i');
  42. ?>

it gives me "cb is undefined" error dont know what is wrong ?????????
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 167
Reputation: Drew is an unknown quantity at this point 
Solved Threads: 7
Drew's Avatar
Drew Drew is offline Offline
Junior Poster

Re: Checkbox Help

 
0
  #2
Nov 19th, 2008
Here is a new function that actually loops through the checkboxes:

  1. function updateopen() {
  2. var c_value = "";
  3. var cb = document.getres.checkbox;
  4.  
  5. for (var i=0; i < document.getres.length ; i++) {
  6. if (document.myForm[i].type=="checkbox" && document.getres.checkbox[i].checked) {
  7. c_value += document.getres.checkbox[i].value + "\n";
  8. };
  9. };
  10.  
  11. alert(c_value);
  12. //window.opener.document.getElementbyId(numbe33).value = c_value
  13. //window.close();
  14. }
Drew Gauderman
ASP / MSSQL Coder
http://www.iportalx.net - My ASP Portal
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 24
Reputation: sasankasekhar is an unknown quantity at this point 
Solved Threads: 3
sasankasekhar's Avatar
sasankasekhar sasankasekhar is offline Offline
Newbie Poster

Re: Checkbox Help

 
0
  #3
Nov 21st, 2008
Originally Posted by farahphp View Post
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1.  
  2. <html>
  3. <head>
  4. <script language=javascript>
  5. function updateopen()
  6. {
  7. var c_value = "";
  8. var cb = document.getres.checkbox;
  9. for (var i=0; i < cb.length ; i++)
  10. {
  11. if (document.getres.checkbox[i].checked)
  12. {
  13. c_value = c_value + document.getres.checkbox[i].value + "\n";
  14. }
  15. }
  16. alert(c_value);
  17. //window.opener.document.getElementbyId(numbe33).value = c_value
  18. //window.close();
  19. }
  20. </script>
  21. </head>
  22. <body>
  23. <?
  24. echo '<form name="getres" id="getres">
  25. <p align=center><table border=1 cellpadding=3 cellspacing=0><tr>
  26. <th width=100px valign=bottom nowrap>
  27. <font face=arial size=3><br> Item Number</font></th>
  28. <th colspan=2 widht=200px valign=bottom nowrap><font face=arial size=3><br> Item Name </font></th></tr>';
  29.  
  30. for ($g=0;$g < sizeof($a); $g++)
  31. {
  32. echo '<tr><td align=middle name=number[] id=number nowrap><font size=2>'.$a[$g].'&nbsp;</font></td>';
  33. echo '<td align=middle name=itname[] id=itname nowrap><font size=2>'.$b[$g].'&nbsp;</font></td>';
  34. echo '<td><input type=checkbox name=checkbox[] value='.$a[$g].'></td></tr>';
  35. }
  36. echo '</table><br>';
  37. echo '<input type=button value=go onClick="updateopen()"></p>';
  38.  
  39. echo '</form></body></html>';
  40. }
  41. include('Spec_dbclose.i');
  42. ?>

it gives me "cb is undefined" error dont know what is wrong ?????????

"cb is undefined"

Your problem is document.getres.checkbox is not giving you a list of checkboxes.
Instead I suggest you to loop through the rows of of the <Table> where you have placed your chekcboxes.
What you have to do
Assign an ID to the TABLE (<TABLE id = "holder">)

and below is your updated function:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function updateopen()
  2. {
  3. var c_value = "";
  4. var cb = document.getElementsByName('checkbox');
  5. var tbl = document.getElementById("holder");//This will get u the Table
  6. var tbody = tbl.childNodes[0];//This will get u the TBODY tag
  7. var rows = tbody.childNodes;//This will get u the list of all the Rows in the TABLE
  8. //alert(tbl.childNodes[0].tagName);
  9.  
  10. //for (var i=0; i < cb.length ; i++)
  11. for (var i=1; i < rows.length ; i++)//We start from i=1 not 0 because the first row dows not contain checkboxes
  12. {
  13. // if (document.getres.checkbox[i].checked)
  14. if(rows[i].childNodes[2].childNodes[0].checked)
  15. {
  16. //c_value = c_value + document.getres.checkbox[i].value + "\n";
  17. c_value = c_value + rows[i].childNodes[2].childNodes[0].value + "\n";
  18. }
  19. }
  20. alert(c_value);
  21. //window.opener.document.getElementbyId(numbe33).value = c_value
  22. //window.close();
  23. }

Regards ...
IF SOMEONE FEELS THAT THEY HAD NEVER MADE A MISTAKE IN THEIR LIFE, THEN  IT MEANS THEY HAD NEVER TRIED A NEW THING IN THEIR LIFE
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC