Jump to content

Building A Registration Form With Array And Foreach.


pioneerx01

Recommended Posts

I am building a registration form where users will have to fill out about 20 different fields. Since each field will be formatted the same I would like to do it with array and foreach statements to reduce code. For example:

 

$field_array = array("first name", "last name", "company");

foreach ($field_array as $field_name)

{

$field_name_upcase = ucwords($field_name);

$field_name_underscore = str_replace(' ', '_', $field_name);

$field_name_error = $field_name_underscore . '_error'; <--- This is wrong!

 

echo "<h6 $field_name_error>$field_name_upcase: <em id='required' $field_name_error>(required)</em></h6>";

echo "<input name='$field_name_underscore' type='text' id='text' value='$$field_name_underscore'/>";

}

 

I would like to be able to put in strings called "$field_name_underscore" that actually works, so it starts looking for $first_name_underscore, $last_name_underscore,...

 

Is there a way? I am not sure how to make a strings from text only.

the process you need to go through is input the the $_POST fields into an array.

 

$varible1 = $_POST['first_name'];

$varible2 = $_POST['last_name'];

$varible3 = $_POST['company'];

$varible4 = $_POST['email'];

 

$array = array($varible1, $varible2, $varible3, $varible4,);

 

foreach($array as $B){

 

$c[] = str_replace(' ', '_', $B);

 

 

}

 

 

this will gie you a formatted array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.