helloise Posted January 4, 2011 Share Posted January 4, 2011 i have this 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();?>" /></td> <td width='200px'><input type="checkbox" name="text[]" value="<?php echo $product_row->getId();?>" /></td> </tr> <?php } ?> </table> <table> <tr> <td> <input type='hidden' name='submit' value='Submit'> <?php if ( (isset($graphic)) || (isset($text) )) { echo "checkboxes checked"; //show submit button } else { //hide submit botton and echo "no boxes check"; } ?> </td> </tr> </table> so if i want to loop through grapic[] with the code: line 36 $graphics_selected = $_POST['graphic']; line 37 foreach($graphics_selected as $val) { if there is something in array(thus any checkbox is checked) enable submit button else if no checkboxes are checked disable submit button } i get errors: Notice: Undefined index: graphic in /home/helloises/svn_wappool/apps/lpmanager/modules/doc/templates/productSuccess.php on line 36 Warning: Invalid argument supplied for foreach() in /home/helloises/svn_wappool/apps/lpmanager/modules/doc/templates/productSuccess.php on line 37 please help???? Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/ Share on other sites More sharing options...
denno020 Posted January 4, 2011 Share Posted January 4, 2011 Where are your <form> tags? Denno Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1154680 Share on other sites More sharing options...
helloise Posted January 5, 2011 Author Share Posted January 5, 2011 here are the whole .php code <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> </tr> <tr> <table width='600px' class='tbl_content_box' border='0' cellspacing='0'> <tr> <td width='100px'>Products:</td> <td width='100px'>Graphic</td> <td width='100px'>Text</td> </tr> </table> <table> <table width='600px' class='tbl_content_box' border='0' cellspacing='0'><?php $product_names = LpmPagePeer::getByAllProdNames($service->getId()); 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();?>" /></td> <td width='200px'><input type="checkbox" name="text[]" value="<?php echo $product_row->getId();?>" /></td> </tr> <?php } ?> </table> <table> <tr> <td> <input type='hidden' name='submit' value='Submit'> <?php $graphics_selected = $_POST['graphic']; foreach($graphics_selected as $val) { echo "blablabl"; } if ( (isset($graphic)) || (isset($text) )) { echo "checkboxes checked"; //show submit button } else { //hide submit botton and echo "no boxes check"; } ?> </td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155064 Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 this is basically what your after needs a few tweaks to put your variable back in etcc and to hide the submit button again if there are no checked boxes. <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'; } } } } </script> </head> <form action="#" method="post"> <input type='hidden' name='service_id' value='1'> <table width='1000px' border='0' cellspacing='0'> <tr> <td class='td_header_2' align='left' colspan='2' width='50%'> Available products for service: bla </td> </tr> <tr> <table width='600px' class='tbl_content_box' border='0' cellspacing='0'> <tr> <td width='100px'>Products:</td> <td width='100px'>Graphic</td> <td width='100px'>Text</td> </tr> </table> <table> <table width='600px' class='tbl_content_box' border='0' cellspacing='0'><?php $product_names = array(1=>"one",2=>"two"); foreach($product_names as $k=>$v) { ?> <tr> <td > </td><td width='200px'>bla</td> <td width='200px'><input type="checkbox" name="graphic[]" value="<?=$k?>" onclick="check()"/></td> <td width='200px'><input type="checkbox" name="text[]" value="2" onclick="check()"/></td> </tr> <?php } ?> </table> <table> <tr> <td> <input id="submit" type='submit' name='submit' value='Submit' style="display:none"> <?php if($_POST['graphic']){ $graphics_selected = $_POST['graphic']; foreach($graphics_selected as $val) { echo "bla"; } } ?> </td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155089 Share on other sites More sharing options...
helloise Posted January 5, 2011 Author Share Posted January 5, 2011 thank you for the trouble but: i added the code but have no products displayed now cos i dont have the lines: $product_names = LpmPagePeer::getByAllProdNames($service->getId()); foreach($product_names as $product_row) { <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();?>" /></td> <td width='200px'><input type="checkbox" name="text[]" value="<?php echo $product_row->getId();?>" /></td> } i also need the $product_row->getId() cos when i hit the submit button i to go to the next form i need the id's of the products(kept in graphic[] and text[]) that where checked to get other info for display on the next form yikes i dont know now..i am a newbie so have no idea on how to go about it...please help thanks! Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155100 Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 your supposed to add the lines i removed back in place of the test arrays i made to test it myself Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155102 Share on other sites More sharing options...
helloise Posted January 5, 2011 Author Share Posted January 5, 2011 thank you yet again, but im lost now....would you mind giving me the full code of how it should be so i can go through it and try to understand it better please??? it will be much appreciated! sorry for the trouble but im still learning all this stuff... many thanks Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155107 Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 <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'; } } } } </script> </head> <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> </tr> <tr> <table width='600px' class='tbl_content_box' border='0' cellspacing='0'> <tr> <td width='100px'>Products:</td> <td width='100px'>Graphic</td> <td width='100px'>Text</td> </tr> </table> <table> <table width='600px' class='tbl_content_box' border='0' cellspacing='0'><?php $product_names = LpmPagePeer::getByAllProdNames($service->getId()); foreach($product_names as $product_row) { ?> <tr> <td > </td><td width='200px'>bla</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 } ?> </table> <table> <tr> <td> <input id="submit" type='submit' name='submit' value='Submit' style="display:none"> <?php if($_POST['graphic']){ $graphics_selected = $_POST['graphic']; foreach($graphics_selected as $val) { echo $val."\n"; } } ?> </td> </tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155114 Share on other sites More sharing options...
helloise Posted January 5, 2011 Author Share Posted January 5, 2011 thank you thank you!!! its working...now just one last thing: how can i check if graphic[] OR text[] contains nothing, thus array is empty??? also the code: if($_POST['graphic']) { $graphics_selected = $_POST['graphic']; //is line 62 foreach($graphics_selected as $val) { echo $val."\n"; } } give error: Undefined index: graphic in /home/helloises/svn_wappool/apps/lpmanager/modules/doc/templates/productSuccess.php on line 62 thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155146 Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 im not sure why your getting the undefined index as i dont have that problem when i run the code. if you want to check on the php side you can use the empty function or javascript use the length function. post ur full code for me to see with regards to that index problem Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155160 Share on other sites More sharing options...
helloise Posted January 5, 2011 Author Share Posted January 5, 2011 the full text is exactly like you gave it to me everything works perfect.. i was thinking maybe i can set a variable(boolean) in the javascript part and check that later in the code???(but dont know how to do that though) just to see if the two arrays contain something..because if one of them contain nothing and i hit submit i get errors on the second form <html> <head> <script type="text/javascript"> function check() { var graphics = document.getElementsByName("graphic[]"); for(i=0;i<graphics.length;i++) { if(graphics[i].checked) { [color=red]var gchecked = true;[/color] 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) { [color=red]var tchecked = true;[/color] if(document.getElementById('submit').style.display=='none') { document.getElementById('submit').style.display='block'; } } } } </script> </head> <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> </tr> <tr> <table width='600px' class='tbl_content_box' border='0' cellspacing='0'> <tr> <td width='100px'>Products:</td> <td width='100px'>Graphic</td> <td width='100px'>Text</td> </tr> </table> <table width='600px' class='tbl_content_box' border='0' cellspacing='0'><?php $product_names = LpmPagePeer::getByAllProdNames($service->getId()); 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 } ?> </table> <table> <tr> <td> <input id="submit" type='submit' name='submit' value='Submit' style="display:none"> <?php [color=red] ///if isgchecked or istchecked is false then i must display a warning[/color] ?> </td> </tr> </table> </form> thanks for all your help so far..just this one last hurdle Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155174 Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 hmm well if you make it that when there is no checked boxes the submit dissappears again like i mentioned earlier you wont get an empty array on the next page unless you get a sneaky hacker then you would have to validate alot more. Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155187 Share on other sites More sharing options...
helloise Posted January 5, 2011 Author Share Posted January 5, 2011 ha ha haa the thing is if say for instance only hte graphics array contain checked boxes and the text array doesnt thus the submit button will show, then when i hit the submit button, the text array i passed to the next form is empty and i then will get an error...hope it makes sense...so i need to get around this somehow but dont know how.. some more help please??? thanks! Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155197 Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 change the javascript function check i gave you to this instead its overkill but it will work. function check(){ var none_checked = false; var graphics = document.getElementsByName("graphic[]"); var text = document.getElementsByName("text[]"); for(i=0;i<graphics.length;i++){ if(!graphics[i].checked){ none_checked = true; } } for(i=0;i<text.length;i++){ if(!text[i].checked){ none_checked = true; } } if(none_checked == true){ document.getElementById('submit').style.display='none'; }else{ document.getElementById('submit').style.display='block'; } for(i=0;i<graphics.length;i++){ if(graphics[i].checked){ if(document.getElementById('submit').style.display=='none'){ document.getElementById('submit').style.display='block'; } } } for(i=0;i<text.length;i++){ if(text[i].checked){ if(document.getElementById('submit').style.display=='none'){ document.getElementById('submit').style.display='block'; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155201 Share on other sites More sharing options...
jake2891 Posted January 5, 2011 Share Posted January 5, 2011 just do this on the next form <?php if($_POST['text']){ echo 'got text'; }else{ echo 'no text'; } if($_POST['graphic']){ echo 'got graphic'; // can do something now }else{ echo 'no graphic'; // cant do shit } ?> Quote Link to comment https://forums.phpfreaks.com/topic/223365-loop-through-checkbox-array-problem/#findComment-1155206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.