Jump to content

How do you use arrays to display a form??


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 post
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>

 

Link to post
Share on other sites
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?

Link to post
Share on other sites
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
Link to post
Share on other sites
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.

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

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

 

Link to post
Share on other sites

Adding the following line of code inside your foreach loop may help with understanding what's inside $value:

echo '<pre>' . print_r($value, true) . '</pre>';

 

Edited by cyberRobot
Link to post
Share on other sites

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.