Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to pass an array

Status
Not open for further replies.

monion03

Programmer
Apr 24, 2007
7
0
0
GR
Hi there,

I wrote the following code:
Code:
<html>
	<head>
		<title> A21P7 </title>
	</head>
	<body>
		<form action="A21P7.php" method="post" >
		<?php
			$Presidents = array('Washington,George', 'Adams,John', 'Jefferson,Thomas', 'Madison,James');
			print "<b>Selection  President</b><BR>";
			foreach ( $Presidents as $index => $person ) {
				print "<input type=\"checkbox\" name=\"chairman[]\" value=$person> $person";
				print "<br/>";
			}
		?>
		</form>
	</body>
</html>

and

Code:
<html>
	<head>
		<title>A21P7</title>
	</head>
	<body>
	<?php
		$chairman = $_POST["chairman"];
		$choice = $_POST["buttonpressed"];
		
		$presidents = array(
		'Washington,George'=> array('Term' => '1789-1797', 'Brief Description' => 'Soldier, statesman, farmer, politician, and commander and chief of the Continental Army during the Revolutionary War.'), 
		'Adams,John' => array('Term' => '1797-1801', 'Brief Description' => 'Farmer, lawyer, political philosopher, diplomat, and politician. He was a leader for U.S. Independence.'), 
		'Jefferson,Thomas' => array('Term' => '1801-1809', 'Brief Description' => 'Architect, scientist, diplomat, author, and politician. He authored the Declaration of Independence.'), 
		'Madison,James' => array('Term' => '1809-1817','Brief Description' => 'One of the framers of the Virginia Constitution, the U.S. Bill of Rights, the U.S. Constitution, and the Federalists Essays.'));


				if (!(count($chairman) == 0)){
				print "<table border=1> <th> Name <th> Term <th> Brief description";
				print "<tr> <td> $chairman </td>";
				print "<td> {$presidents[$chairman]['Term']} </td>";
				print "<td> {$presidents[$chairman]['Brief Description']} </td>";
			} else {
				print "No such president = $chairman ";
			}
		
	?> 
	</body>
</html>

The problem is that i cannot pass all the checked presidents in order to print them. A warning occurs "Warning: Illegal offset type in ". Does anyone know how to solve this problem??

Thanks a lot!
 
plot your form like this instead

Code:
print "<input type=\"checkbox\" name=\"chairman[[red]$person[/red]\" > $person";

do not rely on browsers sending the value of a checkbox, other than a true/false. some do, some dont.

then your output code will need to be a bit tweaked because you are using checkboxes and so multiple selections are availale

Code:
if (count($chairman) > 0){
  foreach ($president as $p){
    if (isset($chairman[$p])){
     //output some data
   } 
 }
}
 
I tried your suggestions and everything works properly. Thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top