Php Form Using Array For Field Name Template User Information
- Forums
- Snippets
- Php Form Using Array For Field Name Template User Information
this is php form using array for field name template user information you can use [1375], Last Updated: Sat May 18, 2024
wallpaperama
Fri Nov 23, 2007
1 Comments
2019 Visits
i often use this form template when i want to create a new form
<style type="text/css">
<!--
form#checkout div.row div.field
{
float: left;
width: 250px;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
}
div.clear
{
clear: both;
}
b {
color: #000000;
}
-->
</style>
<?php
$num_fields = 18;
function tidy ($form_string) {
$form_string = trim(stripslashes($form_string));
return $form_string;
}
?>
<form action="" method="post" id="checkout">
<?php
$field_name = array(
1 => 'Company Name:(if any)',
2 => 'First Name',
3 => 'Last Name',
4 => 'Street',
5 => 'City',
6 => 'State/ Providence',
7 => 'Country',
8 => 'Zip/Postal Code',
9 => 'Phone',
10 => 'E-Mail',
11=> 'Confirm E-Mail',
12 => '<p> </p>',
13 => 'Password',
14 => 'Confirm Password',
15=> 'Secret Question',
16 => 'Answer',
17 => 'Confirmation Key',
18 => 'Agree To Terms of Service'
);
$field_type = array(
1 => 'text',
2 => 'text',
3 => 'text',
4 => 'text',
5 => 'select',
6 => 'select',
7 => 'text',
8 => 'text',
9 => 'text',
10 => 'text',
11=> 'text',
12 => 'break',
13 => 'password',
14 => 'password',
15 => 'text',
16 => 'text',
17 => 'text',
18 => 'checkbox'
);
for($counter =1; $counter <= $num_fields ; $counter++)
{
echo '<div class="row"><div class="field">'.$field_name[$counter].'</div>';
switch($field_type[$counter])
{
case 'textarea':
echo '<textarea name="user['.$counter.']">'.$_POST['user'][$counter].'</textarea>';
break;
case 'checkbox':
echo '<input type="'.$field_type[$counter].'" name="user['.$counter.']" value="'.$_POST['user'][$counter].'">';
break;
case 'radiobutton':
echo '<input type="'.$field_type[$counter].'" name="user['.$counter.']" value="'.$_POST['user'][$counter].'">';
break;
case 'select':
echo'
<select name="question" >
<option value="What is your favorite pets name?">What is your favorite pets name?</option>
<option value="What is your fathers birth date?">What is your fathers birth date?</option>
<option value="What is your favorite music band?">What is your favorite music band?</option>
<option value="What is your mothers maiden name?">What is your mothers maiden name?</option>
</select>
';
break;
case 'file':
echo '<input type="'.$field_type[$counter].'" name="user['.$counter.']" value="'.$_POST['user'][$counter].'">';
break;
case 'hidden':
echo '<input type="'.$field_type[$counter].'" name="user['.$counter.']" value="'.$_POST['user'][$counter].'">';
break;
case 'break':
break;
default:
echo '<input type="'.$field_type[$counter].'" name="user['.$counter.']" value="'.$_POST['user'][$counter].'">';
break;
}
echo'</div><div class="clear"></div></div>';
}
?>
<input type=submit name="Submit" value="Submit" />
</form>
<?php
// The data comes from the form in fig 4.2
if(isset($_REQUEST['Submit']))
{
foreach($_POST['user'] as $key => $line) {
$user = tidy($line);
print("n<br />User[" . $key . '] ' . $user);
}
}
?>
often you're unsure whether the site will be popular or not, so it's best not to do things like the above.