Jump to content

Checkbox and text box array


Gokul

Recommended Posts

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

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

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.

 

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.....

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.