Skip to content Skip to sidebar Skip to footer

How Do I Create An Array From Form Input

I'm trying to write a php script that takes the input from this form and creates an array with a unique key for each input box/text boxes content.

Solution 1:

I think you want to use a form as an array and then pass the same data as an array.If you are using the $_POST, you will need to use the name attribute of HTML. And use something like this :

<input name="person[1][first_name]" value="jane" />

This when passing through PHP will be sent as Array. Here is the reference : http://php.net/manual/en/reserved.variables.post.php An Excerpt from one of the contributors :

<form><inputname="person[0][first_name]"value="john" /><inputname="person[0][last_name]"value="smith" /><inputname="person[1][first_name]"value="jane" /><inputname="person[1][last_name]"value="jones" /></form>

When Trying To Process Values using PHP

<?php
var_dump($_POST['person']);
//will get you something like:array (
0 => array('first_name'=>'john','last_name'=>'smith'),
1 => array('first_name'=>'jane','last_name'=>'jones'),
)
?>

Post a Comment for "How Do I Create An Array From Form Input"