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> </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
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 evenname[foo], then access from PHP using$_POST['name']['foo'].