Josee Posted January 1, 2012 Share Posted January 1, 2012 Hello, First, my very best wishes to everyone for this new year 2012. … And my first question for 2012! I'm trying to enlarge the create_form_input() function from chapter 4 so that it also takes care of labels. I've managed to do practically everything I want it to do, except that I would like the $label_text argument to have a default value, and I haven't found a solution yet. The idea is that the label text would be based on the $name variable in this way: $label_text = ucwords(str_replace('_', ' ', $name)); If there was only English to be taken into account, I could just hardcode it into the function. There would be no $label_text parameter, and I would just write: <label for="'.$name.'">'.ucwords(str_replace('_', ' ', $name)).'</label> so that if the value of $name is 'first_name', the label text is 'First Name'. But, since I want this function to handle the label text whatever language is used, I need a $label_text argument. I first thought of making ucwords(str_replace('_', ' ', $name)) the default value for the $label_text parameter, but I don't know how to achieve this since the PHP manual states that "The default value must be a constant expression, not (for example) a variable, a class member or a function call." I then thought of passing this function parameter by reference, and the beginning of the function now looks like this: function create_form_input2($name, $type, $size_rows, $maxlength_cols, $label_class, &$label_text, $input_class, $errors, $span_class='') { $value = false; if (isset($_POST[$name])) $value = $_POST[$name]; $label_text = ucwords(str_replace('_', ' ', $name)); … It works if I keep the variable for this parameter: create_form_input2('first_name', 'text', '30', '30', 'left', $label_text, 'margin_left', $erreurs); This is what I get: But if I try using a string, I get the following error message: Fatal error: Cannot pass parameter 6 by reference in …ecom1/index.php5 on line 27 Line 27 being: create_form_input2('first_name', 'text', '30', '30', 'above', 'Prénom', 'below', $erreurs); which means that I still don't understand "references" . If someone could help me find a solution, I'd be really grateful. Below: first, a few CSS classes I created so that I could either have the label and the input on the same line (as on the screen shot above), or the input below the label. Then, my attempt at rewriting the function. Then the form. ############### # CSS classes # ############### form label.left, form span.left { position: absolute; width: 8em; font-size: 1em; text-align: right; } form input.margin_left, form textarea.margin_left { margin-left: 8.7em; text-align: left; } form label.above, form input.below, form textarea.below, form span.above { display: block; } ####################### # The function itself # ####################### function create_form_input2($name, $type, $size_rows, $maxlength_cols, $label_class, &$label_text, $input_class, $errors, $span_class='') { $value = false; if (isset($_POST[$name])) $value = $_POST[$name]; // Label text based on $name. $label_text = ucwords(str_replace('_', ' ', $name)); if ($value && get_magic_quotes_gpc()) $value = stripslashes($value); // Check the input type. if (($type == 'text') || ($type == 'password')) { // Create the input. echo '<p><label class="'.$label_class.'" for="'.$name.'"> '.$label_text.'</label>'; echo '<input type="'.$type.'" name="'.$name.'" id="'.$name.'" size="'.$size_rows.'" maxlength="'.$maxlength_cols.'" '; // Add the input's value, if applicable. if ($value) echo 'value="'.htmlspecialchars($value).'"'; echo ' class="'.$input_class; // Check for an error. if (array_key_exists($name, $errors)) { echo ' error" /> <span class="error '.$span_class.'">' .$errors[$name].'</span></p>'; } else { echo '" /></p>'; } } // Check if the input type is a textarea. elseif ($type == 'textarea') { echo '<p><label class="'.$label_class.'" for="'.$name.'">' .$label_text.'</label>'; // Display the error first, so that it's more obvious. if (array_key_exists($name, $errors)) echo '<br /> <span class="error '.$span_class.'">'.$errors[$name].'</span>'; // Create the textarea. echo '<textarea name="'.$name.'" id="'.$name.'" rows="'.$size_rows.'" cols="'.$maxlength_cols.'" class="'.$input_class; // Add the 'error' class, if applicable. if (array_key_exists($name, $errors)) { echo ' error">'; } else { echo '">'; } // Add the value to the textarea. if ($value) echo $value; echo '<textarea></p>'; } // End of primary IF-ELSE. } // End of the create_form_input() function. ######## # Form # ######## <form method="post" action="" accept-charset="utf-8"> <?php $erreurs = array ('first_name' => 'Please enter your first name.', 'last_name' => 'Please enter your last name.', 'comment' => 'Please enter your comment.'); // The first three function calls work and produce the screen shot above. create_form_input2('first_name', 'text', '30', '30', 'left', $label_text, 'margin_left', $erreurs); create_form_input2('last_name', 'text', '30', '30', 'left', $label_text, 'margin_left', $erreurs); create_form_input2('comment', 'textarea', '10', '70', 'left', $label_text, 'margin_left', $erreurs, 'left'); // This is what triggers an error message: create_form_input2('first_name', 'text', '30', '30', 'above', 'Prénom', 'below', $erreurs); create_form_input2('last_name', 'text', '30', '30', 'above', 'Nom', 'below', $erreurs); create_form_input2('comment', 'textarea', '10', '70', 'above', 'Échos', 'below', $erreurs, 'above'); ?> <input type="submit" name="submit" value="Submit!" class="formbutton clear" /> </form> With thanks for your help, Link to comment Share on other sites More sharing options...
Larry Posted January 3, 2012 Share Posted January 3, 2012 Hey Josee, I do question whether it makes sense to auto-generate the labels, but in any case, I'm not sure why your solution depends upon the language in use. Just give $label a default of false. Then, in the function, if $label equals false, do your variable to label trick; otherwise, use the provided label value. Also remember that the parameters that are optional--that have default values--should always go last in the function definition. Link to comment Share on other sites More sharing options...
Josee Posted January 3, 2012 Author Share Posted January 3, 2012 Thank you, Larry. Your solution is perfect for me. What would be your reservations about auto-generating the labels? Link to comment Share on other sites More sharing options...
Recommended Posts