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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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