-2

Can you send an array with various values in html? I would like to send different array values with various different submit buttons all within one <form> element.

Here is what I am doing currently. It works so I'm not having a problem, but I couldn't find any documentation for anything similar and I am really curious if theres another way.

Button with my *psuedo*array

 <input type="submit" name="form_action" value="action:new_business,id:0">

Decode function:

$action = explode(',', $_POST['form_action']);
$new = array();
foreach ($action as $v) {
    $t = explode(':',$v);
    $new[$t[0]] = $t[1];
}
print_r($new);

And the results:

Array ( [action] => new_business [id] => 0 )

Of course, this works, so I'm really just curious whether there's a built in solution already.

The desired simplicity:

<input type="submit" name="array" value="array('0'=>'foo','1'=>'bar')">

print_r($_POST['array]);

Array ( [0] => foo [1] => bar )

Edit: I know how to send arrays with html, but that was not my question. If I use hidden inputs, they get sent regardless of which submit button I click, there will be multiple submit buttons contained in one <form> element, and I need to know which was clicked and what action it is going to be used for. Sorry if that was unclear but I don't think I deserve downvotes either way...

jamil
  • 555
  • 5
  • 18
  • This may have already been answered as it was asked differently [here](http://stackoverflow.com/questions/13885527/passing-arrays-from-html-form-to-php). – oomlaut May 15 '13 at 19:34

2 Answers2

3

Try this:

<input type="hidden" name="form_action[action]" value="new_business" />
<input type="hidden" name="form_action[id]" value="0" />

Inputs with names of the form name[key] will be condensed into an array. This also applies to name[], which will become elements of an indexed array.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Please see my edit, this will not work because everything will be under one form and there will be multiple "submit" buttons with different actions and different IDs. Sorry if that was unclear. – jamil May 15 '13 at 19:43
  • In that case you're probably best with your current setup. – Niet the Dark Absol May 15 '13 at 19:50
  • Damn, pretty harsh response! I'm sorry I shared the question lol I was just hoping there was a different way. Thanks for your efforts, Kolink. – jamil May 15 '13 at 19:56
0

I know the question is old. I still like to answer this, as I am implementing currently something similar.

You can indeed write your statement and turn it into a valid context. By implementing:

if(isset($_POST['array'])){
   eval('$my = '.$_POST['array'].';');
   print_r($my);
}

you concatenate your POST to a valid php expression in string format and evaluate it. This however is very dangerous because it allows execution of any code inside without any verification. You MUST NOT use this in a public environment because anyone can easily modify the string being send by the button.

The best solution in terms of safety and efficiency is really to send a csv format:

<input type="submit" name="array" value="array('0','foo','1','bar')">

in php do:

$my_assoc = array();            
if(isset($_POST['action'])){
    $my = explode(",",filter_input(INPUT_POST, 'action' , FILTER_SANITIZE_STRING));
    for($i = 0, $num = count($my); $i < $num; $i+=2){ 
        $my_assoc[$my[$i]] = $my[$i+1]; 
    }
    print_r($my_assoc);
}

The explode function is linear in complexity and has no large impact. By filtering the csv string, you can also make sure to have no unwanted characters in it (never trust incoming data). You can then either keep the indexed array ($my) and treat every two values as (psydo) key-value pair or turn it into an associative array ($my_assoc).

phx16
  • 37
  • 5