pioneerx01 Posted November 9, 2012 Share Posted November 9, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270482-building-a-registration-form-with-array-and-foreach/ Share on other sites More sharing options...
JamesThePanda Posted November 9, 2012 Share Posted November 9, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/270482-building-a-registration-form-with-array-and-foreach/#findComment-1391217 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.