Jump to content

Check box arrays


koushikgattu

Recommended Posts

Hi, i am a beginner.  I am having problem while printing the check box arrays and with the joining of arrays.

 

here is the sample code:

<?php

 

$retval = '<form name="myform action="post"><input type="checkbox" name="array1[]" value="elem1" checked >feed1</label><br/>';

$retval .= '<input type="checkbox" name="array1[]" value="elem2" checked >feed2</label><br/>';

$retval .= '<input type="checkbox" name="array2[]" value="elem3" checked >feed3</label><br/>';

$retval .= '<input type="checkbox" name="array3[]" value="elem4" checked >feed4</label></form>';

 

echo $_POST['array1[]'];

print_r($_POST['array1[]']);

//print_r($array2);

//print_r($array3);

 

?>

 

 

I am trying to print the array1[], array2[], and array3[] and also put the elements of these arrays into one array(with out override).Can somebody help me with this?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/80166-check-box-arrays/
Share on other sites

try this. i think it is what you want:

 

<?php
if($_POST['submit']) {
	$array = $_POST['array'];
	print 'You have selected these items:<ul>';
	foreach($array as $item) {
		print $item . '<br />';
	}
	print '</ul>';
}
else {
	print '<form name=' . $_SERVER['PHP_SELF'] . ' method=post>';
	print '<label for=item1>Item 1:</label><input type=checkbox name=array[] value=item1 checked><br />';
	print '<label for=item1>Item 2:</label><input type=checkbox name=array[] value=item2 checked><br />';
	print '<label for=item1>Item 3:</label><input type=checkbox name=array[] value=item3 checked><br />';
	print '<label for=item1>Item 4:</label><input type=checkbox name=array[] value=item4 checked><br />';
	print '<label for=item1>Item 5:</label><input type=checkbox name=array[] value=item5 checked><br />';
	print '<input type=submit name=submit value=Submit />';
}
?>

Link to comment
https://forums.phpfreaks.com/topic/80166-check-box-arrays/#findComment-406312
Share on other sites

<?php

$retval = '<form name="myform action="post"><input type="checkbox" name="array1[]" value="elem1" checked >feed1</label>
';
$retval .= '<input type="checkbox" name="array1[]" value="elem2" checked >feed2</label>
';
$retval .= '<input type="checkbox" name="array1[]" value="elem3" checked >feed3</label>
';
$retval .= '<input type="checkbox" name="array1[]" value="elem4" checked >feed4</label></form>';

//echo $_POST['array1[]'];
print_r($_POST['array1']);
//print_r($array2);
//print_r($array3);

?>

 

Putting all the values into just array1 will give you 3 elements after POSTing

$_POST['array1'][0] = elem2

$_POST['array1'][1] = elem3

$_POST['array1'][2] = elem4

 

No merging necessary...

 

PhREEEk

Link to comment
https://forums.phpfreaks.com/topic/80166-check-box-arrays/#findComment-406348
Share on other sites

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.