Gokul Posted February 24, 2010 Share Posted February 24, 2010 Hello all php people. Many apologies if this has been asked before (which it most probably has) but I've been struggling with this for a while now and would greatly appreciate any help. Tried searching google and forums but cannot seem to find much info, and what I have found didn't make much sense to me. For this example, I have created a simple form with checkboxes and text boxes show below: - <?php echo "<form action=\"$_SERVER[php_SELF]\" method=\"POST\">\r"; for($i=0;$i<=5;$i++) { echo "<input type=\"checkbox\" name=\"checked[]\" value=\"$i\" />\r Notes <input type=\"textbox\" name=\"notes[]\" size=\"50\" /><hr />\r"; } echo "<input type=\"Submit\" name=\"submit\" value=\"Submit\" />\r </form>"; $checked = $_POST['checked']; $total = count($checked); if(isset($_POST['submit'])) { for($j=0;$j<=$total;$j++) { echo "$checked[$j] – ".$_POST['notes'][$j]."<br />"; } } ?> I just want to simply loop through the form and get the value of the text box only when its checkbox is checked. The for loop I have written is me trying to experiment with retrieving the checkbox value along with its relevant text box value but am not having much luck. Thanks in advance for this. Link to comment https://forums.phpfreaks.com/topic/193194-checkbox-and-text-box-array/ Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 Something like this is probably a bit more accurate than what you did. Note the foreach to loop through the checkbox array and using the value to get its note. That way you only show a note whose corresponding box was checked and not all of them. for($i=1;$i<=5;$i++) { echo '<form action="" method="POST">'; echo "<input type=\"checkbox\" name=\"box[$i]\" value=\"$i\" />\r Notes <input type=\"textbox\" name=\"note[$i]\" size=\"50\" /><hr />\r"; } echo "<input type=\"Submit\" name=\"submit\" value=\"Submit\" />\r </form>"; $box = $_POST['box']; $note = $_POST['note']; $total = count($box); if(isset($_POST['submit'])) { foreach ($box as $checked) { echo "{$note[$checked]}<br>"; } } HTH Tetamatomic Link to comment https://forums.phpfreaks.com/topic/193194-checkbox-and-text-box-array/#findComment-1017381 Share on other sites More sharing options...
Gokul Posted February 24, 2010 Author Share Posted February 24, 2010 Hi Teamatomic. Many thanks for your reply and yes it nearly solves the problem however, a warning message appears when the submit button is pressed and no checkboxes are checked. I don't think there is anyway to suppress warning messages for the foreach loop according to the PHP.net manual. Link to comment https://forums.phpfreaks.com/topic/193194-checkbox-and-text-box-array/#findComment-1017398 Share on other sites More sharing options...
Wolphie Posted February 24, 2010 Share Posted February 24, 2010 Of course there is, first check that it's an array you're trying to loop using http://php.net/manual/en/function.is-array.php Link to comment https://forums.phpfreaks.com/topic/193194-checkbox-and-text-box-array/#findComment-1017399 Share on other sites More sharing options...
Gokul Posted February 24, 2010 Author Share Posted February 24, 2010 Hi Wolhpie. Ahh that's brilliant. So you're saying to check and see if the checkbox is an array and if it is then execute the rest of the code. Many thanks to both for your help as I was stuck for ages. Link to comment https://forums.phpfreaks.com/topic/193194-checkbox-and-text-box-array/#findComment-1017406 Share on other sites More sharing options...
Gokul Posted February 24, 2010 Author Share Posted February 24, 2010 Hello again. I forgot to mention that I need to get the value of the checkbox with reference to the text box. This is because is need to update the database where the ID equals the value of the checkbox. Link to comment https://forums.phpfreaks.com/topic/193194-checkbox-and-text-box-array/#findComment-1017408 Share on other sites More sharing options...
Gokul Posted February 24, 2010 Author Share Posted February 24, 2010 Sorry, I was hasty in not figuring it out myself. Got it now and this is the code <?php echo '<form action="" method="POST">'; for($i=1;$i<=5;$i++) { echo "<input type=\"checkbox\" name=\"box[$i]\" value=\"$i\" />\r Notes <input type=\"textbox\" name=\"note[$i]\" size=\"50\" /><hr />\r"; } echo "<input type=\"Submit\" name=\"submit\" value=\"Submit\" />\r </form>"; $box = $_POST['box']; $note = $_POST['note']; $total = count($box); if(isset($_POST['submit']) && is_array($box)) { foreach ($box as $checked) { echo "Value $checked: $note[$checked]<br>"; } } ?> Many thanks..... Link to comment https://forums.phpfreaks.com/topic/193194-checkbox-and-text-box-array/#findComment-1017415 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.