Jump to content

Recommended Posts

Hey,

So I'm doing a school assignment and my professor wants us to use an array to display forms and I have no idea how to do that.

 

He wants us to create an array of arrays like this:

display_form(
	array(
		"type" => "text",
		"name" => "first_name",
		"value" => "",
		"label" => "First Name"
	),

	array(
		"type" => "text",
		"name" => "last_name",
		"value" => "",
		"label" => "Last Name"
	),

	array(
		"type" => "email",
		"name" => "email",
		"value" => "",
		"label" => "Email"
	),
	array(
		"type" => "number",
		"name" => "extension",
		"value" => "",
		"label" => "Extension"
	)
);

But I dont understand how you use that array to actually display the form. Does anyone know how?

Link to comment
https://forums.phpfreaks.com/topic/311586-how-do-you-use-arrays-to-display-a-form/
Share on other sites

<!DOCTYPE HTML>

<html>

<head>
    <title>Untitled</title>
<style type="text/css">
/* <![CDATA[ */

body{
	background-color:#e4dab8;
}

form fieldset{
	background-color:#fff9e7;

	border-width:2px;
	border-style:solid;
	border-color:#7c5b47;

	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size:12px;

	margin:20px 0px 20px 0px;
	width:350px;
	position:relative;
	display:block;
	padding: 0px 10px 10px 10px;
}

form fieldset legend{
	background-color:#7c5b47;

	border-width:1px;
	border-style:solid;
	border-color:#FFCC99;

	color:#ffcc99;
	font-weight:bold;
	font-variant:small-caps;
	font-size:110%;

	padding:2px 5px;
	margin:0px 0px 10px 0px;
	position:relative;
	top: -12px;

}

form fieldset legend img{
	padding:0px 5px 0px 5px;
}

label{
	font-size:80%;

	display:block;
	float:left;
	width:100px;
	text-align:right;
	margin:6px 5px 0px 0px;
}

.button{
	background-color:#7c5b47;

	border-width:1px;
	border-style:solid;
	border-color:#FFCC99;

	font-weight:bold;
	font-family:Verdana, Arial, Helvetica, sans-serif;
}

/* ]]> */
</style>
</head>

<body>

<form>
  <fieldset>
  <legend>My Array Form Generator </legend>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    
}

// Form Fields Arrays: Field Title, Field Name, Field Type
$formfields = array(
    array(
        "First Name",
        "name_first",
        "text"
    ),
    array(
        "Last Name",
        "name_last",
        "text"
    ),
    array(
        "Username",
        "username",
        "text"
    ),
    array(
        "Password",
        "password",
        "password"
    ),
);

foreach ($formfields as $key => $value)
    {
    echo "<label for='{$value[1]}'>{$value[0]}</label>\n<input id='{$value[1]}' name='{$value[1]} type=\"{$value[2]}' value='' ><br><br>\n\n";
    }

?>
<input type="submit" name="submit" value="Submit" class="button">
  </fieldset>
</form>
</body>

</html>

 

On 10/10/2020 at 3:30 PM, slotegraafd said:

But I dont understand how you use that array to actually display the form. Does anyone know how?

Where are you stuck? Creating the array or using the array to display the form? What have you done so far? What does your current code look like?

On 10/10/2020 at 4:59 PM, benanamen said:


<!DOCTYPE HTML>

<html>

<head>
    <title>Untitled</title>
<style type="text/css">
/* <![CDATA[ */

body{
	background-color:#e4dab8;
}

form fieldset{
	background-color:#fff9e7;

	border-width:2px;
	border-style:solid;
	border-color:#7c5b47;

	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size:12px;

	margin:20px 0px 20px 0px;
	width:350px;
	position:relative;
	display:block;
	padding: 0px 10px 10px 10px;
}

form fieldset legend{
	background-color:#7c5b47;

	border-width:1px;
	border-style:solid;
	border-color:#FFCC99;

	color:#ffcc99;
	font-weight:bold;
	font-variant:small-caps;
	font-size:110%;

	padding:2px 5px;
	margin:0px 0px 10px 0px;
	position:relative;
	top: -12px;

}

form fieldset legend img{
	padding:0px 5px 0px 5px;
}

label{
	font-size:80%;

	display:block;
	float:left;
	width:100px;
	text-align:right;
	margin:6px 5px 0px 0px;
}

.button{
	background-color:#7c5b47;

	border-width:1px;
	border-style:solid;
	border-color:#FFCC99;

	font-weight:bold;
	font-family:Verdana, Arial, Helvetica, sans-serif;
}

/* ]]> */
</style>
</head>

<body>

<form>
  <fieldset>
  <legend>My Array Form Generator </legend>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    
}

// Form Fields Arrays: Field Title, Field Name, Field Type
$formfields = array(
    array(
        "First Name",
        "name_first",
        "text"
    ),
    array(
        "Last Name",
        "name_last",
        "text"
    ),
    array(
        "Username",
        "username",
        "text"
    ),
    array(
        "Password",
        "password",
        "password"
    ),
);

foreach ($formfields as $key => $value)
    {
    echo "<label for='{$value[1]}'>{$value[0]}</label>\n<input id='{$value[1]}' name='{$value[1]} type=\"{$value[2]}' value='' ><br><br>\n\n";
    }

?>
<input type="submit" name="submit" value="Submit" class="button">
  </fieldset>
</form>
</body>

</html>

 

@benanamen Ah I see, Notice: Undefined offset: 2 in C:\XAMPP\htdocs\Webd3201\salesperson.php on line 83 though do you know what this error means?

Edited by slotegraafd
16 hours ago, slotegraafd said:

Notice: Undefined offset: 2

It sounds like you are trying to use an array key that doesn't exist. Are you using an associative array, like your first post above shows. Or are you using a numeric array, like benanamen's code uses? Your choice will affect how you use the keys within the loop that outputs the form fields.

7 hours ago, cyberRobot said:

It sounds like you are trying to use an array key that doesn't exist. Are you using an associative array, like your first post above shows. Or are you using a numeric array, like benanamen's code uses? Your choice will affect how you use the keys within the loop that outputs the form fields.

Oh so I’m using the array that I have in my code there. (I’m really bad with arrays if you couldn’t tell) I thought that each one in the array corresponded to a number no matter what type of arrays 

6 hours ago, cyberRobot said:

What does your current code look like?

// Form Fields Arrays: Field Title, Field Name, Field Type

$display_form = array(
  array(
    "type" => "text",
    "name" => "first_name",
    "value" => "",
    "label" => "First Name"
  ),
  array(
    "type" => "text",
    "name" => "last_name",
    "value" => "",
    "label" => "Last Name"
  ),
  array(
    "type" => "email",
    "name" => "email",
    "value" => "",
    "label" => "Email"
  ),
  array(
    "type" => "number",
    "name" => "extension",
    "value" => "",
    "label" => "Extension"
  ),
);

foreach ($display_form as $key => $value)
    {
    echo "<label for='{$value[1]}'>{$value[3]}</label>\n<input id='{$value[1]}' name='{$value[1]} type=\"{$value[0]}'  value='{$value[2]}' ><br><br>\n\n";
    }

?>
<input type="submit" name="submit" value="Submit" class="button">
  </fieldset>
</form>

 

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.