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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.