Jump to content

Sending arrays with a HTML form (newbie question)


boff2000

Recommended Posts

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

[code]<?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;
}
?>
[/code]




???
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 [i]you[/i] are wanting it to do. so here is an example:

[code]
<?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>
[/code]

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

[code]<?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;
}

?>[/code]

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.