Lodius2000 Posted May 11, 2008 Share Posted May 11, 2008 here is the code for formhelpers.php <?php //print a text box function input_text($element_name, $values){ print '<input type="text" name="' . $element_name .'" value="'; print htmlentities($values[$element_name]) . '"/>'; } //print a submit button function input_submit($element_name, $label){ print '<input type="submit" name="' . $element_name .'" value="'; print htmlentities($label) . '"/>'; } //print a textarea function input_textarea($element_name, $values){ print '<textarea name="' . $element_name .'">'; print htmlentities($values[$element_name]) . '</textarea>'; } //print a radio button or checkbox function input_radiocheck($type, $element_name, $values, $element_value){ print '<input type="' . $type . '" name="' . $element_name . '" value="' . $element_value . '" '; if ($element_value == $values[$element_name]){ print ' checked="checked"'; } print '/>'; } //print a select menu function input_select($element_name, $selected, $options, $multiple = false){ //print out the <select> tag print '<select name="' . $element_name; // if multiple choices are permitted, add the multiple attribute // and add a [] to the end of the tag name if ($multiple){ print '[]" multiple="multiple'; } print '">'; //set up the list of things to be selected $selected_options = array(); if ($multiple) { foreach ($selected[$element_name] as $val){ $selected_options[$val] = true; } } else { $selected_options[ $selected[$element_name] ] = true; } //print out the <option> tags foreach($options as $option => $label){ print '<option value="' . htmlentities($option) . '"'; if ($selected_options[$option]){ print ' selected="selected"'; } print '>' . htmlentities($label) . '</option>'; } print '</select>'; } ?> and here the code for the script i am writing <?php require 'formhelpers.php'; session_start(); $colors = array('#ffffff' => 'White', '#ff0000' => 'Red', '#00ff00' => 'Green', '#0000ff' => 'Blue', '#000000' => 'Black'); if($_POST['_submit_check']){ if($form_errors = validate_form()){ show_form($form_errors); } else { show_form(); process_form(); } } else { show_form(); } function show_form($errors = '') { print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">'; if ($errors){ print 'Please correct these errors: <ul><li>'; print implode('</li><li>', $errors); print '</li></ul>'; } //begin the unique form print 'Color: '; input_select('color', $_POST, $_GLOBALS[colors]); print '<br />'; input_submit('submit', 'Select Color'); print '<input type="hidden" name="_submit_check" value="1" />'; print '</form>'; } function validate_form(){ $errors = array(); // color selected must be valid if (! array_key_exists($_POST['colors'], $_GLOBALS['colors'])){ $errors = 'please enter a valid color.'; } return $errors; } function process_form(){ $_SESSION['color'] = $_POST['color']; print "your favorite color is " . $_GLOBALS['colors'][$_SESSION['color'] ]; } ?> my question is, why does the select menu not have the colors in $colors when i run the script Thanks Link to comment https://forums.phpfreaks.com/topic/105186-solved-select-menu-has-no-options/ Share on other sites More sharing options...
btherl Posted May 12, 2008 Share Posted May 12, 2008 Try printing out the input values to input_select(), just after the function starts. You can use code like this: print "<pre>"; var_dump($options); print "</pre>"; That will verify that the option are being passed into the function. If the options ARE being passed in, then the problem is either with the format of the options or inside input_select() somewhere. If the options are NOT passed in, then the problem is before input_select() is called. That way you narrow down where the problem is. Have you checked the HTML source of the page to see what the color select looks like? Link to comment https://forums.phpfreaks.com/topic/105186-solved-select-menu-has-no-options/#findComment-538653 Share on other sites More sharing options...
Lodius2000 Posted May 12, 2008 Author Share Posted May 12, 2008 Have you checked the HTML source of the page to see what the color select looks like? yeah, the page source looks like this when the page is loaded <form method="POST" action="/phpexercises/ch8ex3.php">Color: <select name="color"><br /> <b>Warning</b>: Invalid argument supplied for foreach() in <b>/formhelpers.php</b> on line <b>49</b><br /> </select><br /><input type="submit" name="submit" value="Select Color"/><input type="hidden" name="_submit_check" value="1" /></form> so i put the code you suggested in there and it printed out NULL <?php require 'formhelpers.php'; session_start(); $colors = array('#ffffff' => 'White', '#ff0000' => 'Red', '#00ff00' => 'Green', '#0000ff' => 'Blue', '#000000' => 'Black'); if($_POST['_submit_check']){ if($form_errors = validate_form()){ show_form($form_errors); } else { show_form(); process_form(); } } else { show_form(); } function show_form($errors = '') { //PRE code here print "<pre>"; var_dump($options); print "</pre>"; print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">'; if ($errors){ print 'Please correct these errors: <ul><li>'; print implode('</li><li>', $errors); print '</li></ul>'; } //begin the unique form print 'Color: '; input_select('color', $_POST, $_GLOBALS[colors]); print '<br />'; input_submit('submit', 'Select Color'); print '<input type="hidden" name="_submit_check" value="1" />'; print '</form>'; } function validate_form(){ $errors = array(); // color selected must be valid if (! array_key_exists($_POST['colors'], $_GLOBALS['colors'])){ $errors = 'please enter a valid color.'; } return $errors; } function process_form(){ $_SESSION['color'] = $_POST['color']; print "your favorite color is " . $_GLOBALS['colors'][$_SESSION['color'] ]; } ?> hopefully that is the right spot, lemme know if it isnt thanks for your help UPDATE:::: line 49 of formhelpers.php is function input_select($element_name, $selected, $options, $multiple = false){ that is of course reffering to the error on line 49 printed when the main script is loaded in a browser Link to comment https://forums.phpfreaks.com/topic/105186-solved-select-menu-has-no-options/#findComment-538700 Share on other sites More sharing options...
Lodius2000 Posted May 12, 2008 Author Share Posted May 12, 2008 SOLVED solution was to change the $_GLOBALS['colors'] to $colors after placing global $colors inside the show_form function thank you for your help btherl Link to comment https://forums.phpfreaks.com/topic/105186-solved-select-menu-has-no-options/#findComment-538710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.