dennismonsewicz Posted February 23, 2010 Share Posted February 23, 2010 I wrote a quick function to generate form fields for me and am stuck on generating checkboxes. Code: function fieldGenerator($title, $fieldName, $type="text", $opts="") { switch($type) { case "submit": $input = '<input type="' . $type . '" name="' . $fieldName . '" value="' . ucwords($fieldName) . '" />'; break; case "checkbox": $input = '<input type="checkbox" value="text" />'; foreach($opts as $name => $value) { //$input .= '<input type="checkbox" value="' . $value . '" name="phoneproblem" />'; } break; case "text": default: $input = '<span>' . ucwords($title) . '</span>'; $input .= '<input type="' . $type . '" name="' . $fieldName . '" id="' . $fieldName . '" />'; break; } return $input; } $ckOptsArray = array( "Lost" => "lost", "Stolen" => "stolen", "Damaged (Dropped, Liquid Damage, Broken, etc)" => "damaged", "Malfunctioning" => "malfunctioning" ); $inputArray = array( "Claim ID Number" => "barcode", "Wireless Device Number" => "deviceNumber", "First Name" => "firstName", "Last Name" => "lastName", "Daytime Phone Number" => "dayNumber", "Evening Phone Number" => "eveningNumber", "E-Mail Address" => "email", "Home Address" => "homeAddy", "City" => "city", "State" => "state", "Zip Code" => "zip", "Device Manufacturer" => "manufacturer", "Model" => "model", "checkbox" => "checkbox", "submit" => "submit" ); //print_r(getimagesize("images/image.jpg")); ?> <form action="generate.php" method="post" id="barcode"> <?php foreach($inputArray as $title => $name) { if($name != "submit") { echo fieldGenerator($title, $name, "text"); } elseif($name == "checkbox") { echo 'Hello!'; echo fieldGenerator($title, $name, "checkbox", $ckOptsArray); } else { echo fieldGenerator($title, $name, "submit"); } } ?> </form> The code above does not print out any checkboxes or the word "hello" in the if else statement. Instead a regular textbox is printed.. any suggestions? Link to comment https://forums.phpfreaks.com/topic/193133-form-field-generator-help/ Share on other sites More sharing options...
dennismonsewicz Posted February 24, 2010 Author Share Posted February 24, 2010 Anyone? I went back to another solution but for future knowledge how could I accomplish this with my code? Link to comment https://forums.phpfreaks.com/topic/193133-form-field-generator-help/#findComment-1017706 Share on other sites More sharing options...
teamatomic Posted February 24, 2010 Share Posted February 24, 2010 because the input is a above the foreach and the other input is commented out. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/193133-form-field-generator-help/#findComment-1017707 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.