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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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