Jump to content

Form Field Generator Help


dennismonsewicz

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

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