sungpeng Posted October 17, 2014 Share Posted October 17, 2014 if($x==""){ $x=1; } while($do=mysql_fetch_array($sql){ <form name='Form' action='posting.php' method='post'> <input type='checkbox' name='<?php print $x; ?>' value='<?php print $do[id]; ?>' /> <input type='submit' name='checked' value='checking'> $x++; } When I click on the submit button, only one value of $x is sent to posting.php instead of multiple loop values in checkbox. Does anyone have a solution to it ? Under Posting.php for ($x = 1; $x <= 20; $x++) { $x=$_POST[$x]; echo "$x"; } Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/ Share on other sites More sharing options...
sungpeng Posted October 17, 2014 Author Share Posted October 17, 2014 if($x==""){ $x=1; } <form name='Form' action='posting.php' method='post'> while($do=mysql_fetch_array($sql){ <input type='checkbox' name='<?php print $x; ?>' value='<?php print $do[id]; ?>' /> $x++; } <input type='submit' name='checked' value='checking'> Still the same problem Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493950 Share on other sites More sharing options...
QuickOldCar Posted October 17, 2014 Share Posted October 17, 2014 Only put in the loop the parts of form need to add new values with php well you did not break out of php and back in, might as well echo in php <form name='Form' action='posting.php' method='post'> <?php if($x==""){ $x=1; } while($do=mysql_fetch_array($sql){ echo "<input type='checkbox' name='".$x."' value='".$do[id]."' />"; $x++; } ?> <input type='submit' name='checked' value='checking'> Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493951 Share on other sites More sharing options...
sungpeng Posted October 17, 2014 Author Share Posted October 17, 2014 No can't work. The posting.php display a word Array. Is there any edition to do with the posting.php page ? Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493952 Share on other sites More sharing options...
sungpeng Posted October 17, 2014 Author Share Posted October 17, 2014 print_r(array_values($array)); still display only one value instead of many loop value. Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493953 Share on other sites More sharing options...
QuickOldCar Posted October 17, 2014 Share Posted October 17, 2014 Maybe this will help, i used a dummy range versus your while loop <form name='Form' method='post'> <?php $dummy_array = range(1, 20); $x = 1; foreach ($dummy_array as $ids) { echo $x . ": <input type='checkbox' name='" . $x . "' value='" . $ids . "' /><br />"; $x++; } ?> <input type='submit' name='checked' value='checking'><br /> <?php //display if (isset($_POST)) { foreach ($_POST as $key => $value) { echo $key . " - " . $value . "<br />"; } } ?> Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493954 Share on other sites More sharing options...
sungpeng Posted October 17, 2014 Author Share Posted October 17, 2014 I need the while loop to fetch data from mysql. If i use a while loop and put a foreach loop inside, it will generate a lot of unnecessary checkbox. Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493955 Share on other sites More sharing options...
sungpeng Posted October 17, 2014 Author Share Posted October 17, 2014 Another question is when i post it over to posting.php, can i just get the staff that is posted. No need to loop through all value to check if it contain value. Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493956 Share on other sites More sharing options...
sungpeng Posted October 17, 2014 Author Share Posted October 17, 2014 foreach ($_POST as $key => $value) { echo "$key"; echo "$value"; } $key and $value are empty. Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493957 Share on other sites More sharing options...
QuickOldCar Posted October 17, 2014 Share Posted October 17, 2014 The while loop is the loop. and correct, just display the value in the $_POST array instead of making each checkbox name a number, you can just make them an array and loop those <form name='Form' method='post'> <?php $dummy_array = range(1,20); foreach($dummy_array as $ids){ echo $ids.": <input type='checkbox' name='staff[]' value='".$ids."' /><br />"; } ?> <input type='submit' name='checked' value='submit'><br /> <?php //display if(isset($_POST['staff'])){ foreach($_POST['staff'] as $staff){ echo $staff."<br />"; } } ?> Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493958 Share on other sites More sharing options...
QuickOldCar Posted October 17, 2014 Share Posted October 17, 2014 So now I will put this together as what your code is, be sure to break in and out of php to html properly or echo the entire form in php code: <?php echo "<form name='Form' action='posting.php' method='post'>"; while($do=mysql_fetch_array($sql){ echo $do['id'].": <input type='checkbox' name='staff[]' value='".$do['id']."' /><br />"; } echo "<input type='submit' name='checked' value=checking'><br />"; ?> posting.php <?php //display on posting.php if(isset($_POST['staff'])){ foreach($_POST['staff'] as $staff){ echo $staff."<br />"; } } ?> Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493960 Share on other sites More sharing options...
sungpeng Posted October 17, 2014 Author Share Posted October 17, 2014 understood, it will get the value of the checked box. Wish to know how to get the value for the unchecked box ? Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493962 Share on other sites More sharing options...
sungpeng Posted October 17, 2014 Author Share Posted October 17, 2014 Sometime we got to check it and sometime uncheck it and update the uncheck in mysql as well. Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493963 Share on other sites More sharing options...
QuickOldCar Posted October 17, 2014 Share Posted October 17, 2014 Do you have a checked/unchecked value or equivalent in your database? This way can mark the checked ones. Taking a stab in the dark here... if($do['checked'] == true){ $checked = "checked='checked'"; }else{ $checked = ""; } echo "<input type='checkbox' name='staff[]' value='".$do['id']."' ".$checked." />"; If you want to pass all the staff regardless to checked or not can send a hidden value. echo "<input type='hidden' name='allstaff[]' value='".$do['id']."' />"; so on display would be $_POST['allstaff'] Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493964 Share on other sites More sharing options...
sungpeng Posted October 17, 2014 Author Share Posted October 17, 2014 Thank You Link to comment https://forums.phpfreaks.com/topic/291871-looping-for-checkbox-in-form/#findComment-1493966 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.