- Forums
- HTML
- How To Make A HTML Web Form Using Array As The Field Name
i will show you how to make a html web form to use arrays as the name of the field name. [751], Last Updated: Sat May 18, 2024
wallpaperama
Tue Jan 20, 2009
0 Comments
732 Visits
today i wanted to make a web form but i wanted the use the name of each field as an array. i know how to do this with php but with html i didnt know how that was going to be possible. well i figure it out so im going to show you how you can do this.
this is how my html looks like:
HTML FORM CODE:
<form action="" method="post" >
Name: <input name="link[]" type="text" id="user_name" value="<? echo $_POST['link'][0]; ?>" size="30" maxlength="30"><br />
Email: <input name="link[]" type="text" id="mail" value="<? echo $_POST['link'][1]; ?>" size="30" maxlength="50"><br />
<input type="submit" name="submit" value="Submit">
on the html code above i only put two field so keep this tutorial short and simple, but if you have alot of fields, you can check each field if it has a value, that way you can check to be sure the user didnt leave any fields empty so i use php to do this
PHP CODE:
<?php
foreach($_POST['link'] as $value){
if (empty($value)){
$error = 'All Fields Are Required';
break;
}
}
if($error){
echo 'All Fields Are Required';
exit;
}
?>
now with the php all all in place i can check each field to be sure its not empty. now i put it all together:
PHP/HTML ARRAY CODE EXAMPLE
<?php
if(isset($_POST['submit'])){
foreach($_POST['link'] as $value){
if (empty($value)){
$error = 'All Fields Are Required';
break;
}
}
if($error){
echo 'All Fields Are Required';
}
}
?>
<form action="" method="post" >
Name:
<input name="link[]" type="text" id="user_name" value="<? echo $_POST['link'][0]; ?>" size="30" maxlength="30">
<br />
Email:
<input name="link[]" type="text" id="mail" value="<? echo $_POST['link'][1]; ?>" size="30" maxlength="50">
<br />
<input type="submit" name="submit" value="Submit">