Jump to content


Photo

Sending arrays with a HTML form (newbie question)


  • Please log in to reply
6 replies to this topic

#1 boff2000

boff2000
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 September 2006 - 03:14 PM

How can I send an array from a form? I try creating a hidden input element with name='array_name' and value='$array_name' , but it doesnt seem to happen for me. I know I'm doing something wrong  :'(

Im quite sure this is totally a newbie question  :-[

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 12 September 2006 - 03:16 PM

name='array_name[]'

Info: PHP Manual


#3 boff2000

boff2000
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 September 2006 - 03:33 PM

hm, ok i tried that to no avail. i think id better paste an example:

<?php

if(!$cmd){
	$array_name = array( "Some text" );
	echo $array_name[0];
}
	
echo "<form action='page.php' method='post'>
	<input type='hidden' name='array_name[]' />
	<input type='submit' name='cmd' value='go' />
	</form>";

switch($_REQUEST['cmd']){ 
    case "go":  
	echo $array_name[0];
    break; 
}
?>




???

#4 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 12 September 2006 - 03:39 PM

<?php

foreach ($array as $value)
{
    echo '<input type="hidden" name="array_name[]" value="' . htmlentities($value) . '" />' . chr(10);
}

?>
[/code]

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 12 September 2006 - 03:52 PM

according to your logic, $array_name will always be set to "some text" whether you click submit or not, because $cmd is never created.  if you have not submitted your form, "some text" will be echoed once: inside your if statement.  if you do submit your form, it will be echoed twice: once inside your if statement, and then again inside your switch statement.  Both times it will still say "some text". Your hidden input named array_name is passed with no value, but it is always overwritten, due to your if statement.

i can plainly see what your example code does. however, i'm not entirely sure what it is you are wanting it to do. so here is an example:

<?php
   if ($_POST['items']) {
      foreach ($_POST['items'] as $val) {
         echo "$val <br>";
      }
   }
?>

<form action = '<?php echo $_SERVER['PHP_SELF']; ?>' method = 'post'>
   <input type = 'hidden' name = 'items[]' value = 'value1'>
   <input type = 'hidden' name = 'items[]' value = 'value2'>
   <input type = 'hidden' name = 'items[]' value = 'value3'>
   <input type = 'submit' name = 'submit' value = 'submit'>
</form>

what this does is creates a form with a submit button.  the 3 hidden input types makes an array called 'items' with 3 values.  when you click submit, it checks to see if items exists, and if so, then loop through and echo all the values. 
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 boff2000

boff2000
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 September 2006 - 05:14 PM

Thanks for the replies! Crayon Violent - yes i understand whats happening - i echoed the the results twice to test if each time returned the same result.

Anyway - for the example i provided, the solutions you guys provided worked for it..

however, i thought i may have been able to apply that logic to what im doing. i actually want to send a multi-dimensional array. The reason being, I want to send it somewhere so that other functions can then edit elements of the array - then ultimately have it all write to XML (but ive got that bit sorted :P )

im doing something wrong about half way through this code.. i bet

<?php
//Create the array only when $cmd does not exist
if(!$cmd){
$arr = array();
$arr[0][0] = "a";
$arr[0][1] = "b";
$arr[1][0] = "y";
$arr[1][1] = "z";	
}

echo "<form action='page.php' method='post'>";

//put the multi-dimensional array into input elements
foreach ($arr as $v1) {
   foreach ($v1 as $v2) {
	echo "<input type='hidden' name='arr[]' value='$v2' />";
	}
}
echo "<input type='submit' name='cmd' value='go' /></form>";

//If $cmd exists, return the array
switch($_REQUEST['cmd']){ 
    case "go":  
	foreach ($arr as $v1) {
   		foreach ($v1 as $v2) {
   			echo $v2;
		}
	}
    break; 
}

?>


#7 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 13 September 2006 - 02:01 AM

foreach ($arr as $k1 => $v1) {
   foreach ($v1 as $k2 => $v2) {
	echo "<input type='hidden' name='arr[$k1][$k2]' value='$v2' />";
	}
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users