Jump to content

javascript function problem with onclick="check()"


helloise

Recommended Posts

i have the code: foreach($product_names as $product_row) { ?> <tr> <td > </td><td width='200px'><?php echo $product_row->getName();?></td> <td width='200px'><input type="checkbox" name="graphic[]" value="<?php echo $product_row->getId();?>" onclick="check()"/></td> <td width='200px'><input type="checkbox" name="text[]" value="<?php echo $product_row->getId();?>" onclick="check()"/></td> </tr> <?php } ?>and a submit button on the page.the i also have the javascript code for the onclick="check()" :<html><head> <script type="text/javascript"> function check() { var graphics = document.getElementsByName("graphic[]"); for(i=0;i<graphics.length;i++) { if(graphics.checked) { if(document.getElementById('submit').style.display=='none') { document.getElementById('submit').style.display='block'; } } } var text = document.getElementsByName("text[]"); for(i=0;i<text.length;i++) { if(text.checked) { if(document.getElementById('submit').style.display=='none') { document.getElementById('submit').style.display='block'; } } } } </script></head>so what all this means is i have two arrays of checkboxes for each product. as soon as i check any checkbox the submit button appears. i click on the submit button and i go to the next form. the problem now comes in if i click on the "back" button to go back to the previous page via the bwrowser..the submit button is disabled althou all my checkboxes that i checked, are still checked....i want the submit button to show???please help??thanks

Link to comment
Share on other sites

Thats probaby because, the function only runs to show the submit button, when that element is clicked, you would probably need to run the function on page 'window.onload', to see if any checkbox is checked, and then, if 'yes' show submit button.
okey dokey thank you but how do i do this??? i am a newbie at all this and have no clue on how to do it, can you show me please so i can learn from this???
Link to comment
Share on other sites

something like this???? BUT as i have it now and i click on the browser page back icon..the theckboxes are still checked buthe submit button does NOT show :) <html><head> <script type="text/javascript"> function check() { var graphics = document.getElementsByName("graphic[]"); for(i=0;i<graphics.length;i++) { if(graphics.checked) { if(document.getElementById('submit').style.display=='none') { document.getElementById('submit').style.display='block'; } } } var text = document.getElementsByName("text[]"); for(i=0;i<text.length;i++) { if(text.checked) { if(document.getElementById('submit').style.display=='none') { document.getElementById('submit').style.display='block'; } } } } function showSubmit() { if((graphics.length >1) || (text.length)) { document.getElementById('submit').disabled = 0; } } window.onload=showSubmit(); </script></head><body onload="showSubmit()"><form action="<?php echo url_for('doc/document'); ?>" method="post"> <input type='hidden' name='service_id' value='<?php echo $service->getId() ?>'><table width='1000px' border='0' cellspacing='0'> <tr> <td class='td_header_2' align='left' colspan='2' width='50%'> Available products for service: <?php echo $service->getName()?> </td> ...the rest of my code here.....</form></body>

Link to comment
Share on other sites

your showSubmit() function does not know what graphics variable relates to, as it does not havevar graphics = document.getElementsByName("graphic[]");But! you don't require another function as you can use your original function to check if checkbox is checked.just use

<html><head><script type="text/javascript">function check(){var graphics = document.getElementsByName("graphic[]");for(i=0;i<graphics.length;i++){if(graphics[i].checked){if(document.getElementById('submit').style.display=='none'){document.getElementById('submit').style.display='block';}}}var text = document.getElementsByName("text[]");for(i=0;i<text.length;i++){if(text[i].checked){if(document.getElementById('submit').style.display=='none'){document.getElementById('submit').style.display='block';}}}}window.onload=check;</script></head><body><form action="<?php echo url_for('doc/document'); ?>" method="post"><input type='hidden' name='service_id' value='<?php echo $service->getId() ?>'><table width='1000px' border='0' cellspacing='0'><tr><td class='td_header_2' align='left' colspan='2' width='50%'>Available products for service: <?php echo $service->getName()?></td>...the rest of my code here.....</form></body>

Link to comment
Share on other sites

thank you so if i understand it correctly i keep my java script code exactly as i had it EXCEPT for the line:window.onload=check;and the rest also exactly the same??so just this ONE line??sorry for the trouble but i am learning all this stuff from scratch :)

Link to comment
Share on other sites

thank you so if i understand it correctly i keep my java script code exactly as i had it EXCEPT for the line:window.onload=check;and the rest also exactly the same??so just this ONE line??sorry for the trouble but i am learning all this stuff from scratch :)
Yep! from what i see of your current code supplied, thats all you require to achive the same effect.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...