0

I have a form that has two field inputs that also have the same name "name"

I was wondering what do I have to do , to capture both fields in array,

for example, if field1 get post and field2 was empty, I would like to get an array[0] with the data I was looking for and if field1 and field2 where submited, I would like to have array[0] and array[1] populated.

currently the form I have will only capture if both fields are populated and if one of them is populated it wonn't capture .

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>


<?php 

$name=$_POST['name'];


if($name)
{

    echo $name;

    }


?>


</head>

<body>


<form action="<?php $_SERVER['PHP_SELF']?>" method="post">

  <p>name
  <input name="name" id="name" type="text" />



  </p>
  Name

  <input name="name"  id="name" type="text" />
  <p>&nbsp;</p>


  <input name="submit" type="submit" />
</form>
</body>
</html>

I would like the end results for the variable name to be an array that would have both data that was capture from field one and field two.

thanks

4
  • 1
    Is there any reason you don't just use different field names? Although many browsers support multiple fields having the same name being passed as a list/array (when appended with []s), it's not an official specification and should be avoided unless there's a good reason Commented Sep 17, 2012 at 20:29
  • There's nothing that says you cannot do this, and I don't know of browsers that don't support it. The [] notation is a PHP addition and shouldn't be needed in the first place. Commented Sep 17, 2012 at 20:33
  • @Basic I have to use the same name cause the field will be a dynamic java script field. for example, if the user want to add another name, they can then click an add button and a new field will show. thanks Commented Sep 17, 2012 at 20:38
  • you could use js to alter the created form inputs to increment the name to name_1 then the next one name_2, but [] is probably going to be easier Commented Sep 17, 2012 at 20:41

1 Answer 1

7

Change the name of each field to name[], then they can be accessed in PHP using $_POST['name'][0] and $_POST['name'][1].

EDIT: You can also use name[1] in your HTML to explicitly set indices, or even name[foo], then access from PHP using $_POST['name']['foo'].

Sign up to request clarification or add additional context in comments.

4 Comments

Basically, the [] in the name turns $_POST['name'] into an array. :-)
Unfortunate that this is needed. It would be perfectly valid to name them just name and still treat them as an array, but PHP hasn't got built in functionality to support that, so adding the brackets is the easiest solution indeed. Edit: I'd add another +1 for the addition about indices, but alas, I cannot.
@GolezTrol - aha, just checked and it does. I assume PHP overwrites by default, since in normal mode, it would have to check the POST data twice and either assign to an array or a field, depending on the number of definitions of each name.
@KhanqueredPro - if it has been useful, please mark it as answered, thanks :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.