Undefined offset error
Posted: Wed Oct 12, 2011 2:14 pm
I'm new to PHP and programming in general. One of the examples in the book I'm reading talks about classes, methods, etc... When I go to localhost/newform.php I get the following error. I have googled it some and understand that the array key/index doesn't exist. I'm just having trouble seeing it. Any help would be greatly appreciated.
Thanks

Here's the newform.php and form.class files.
Filename = form.class
Filename = newform.php
Thanks

Here's the newform.php and form.class files.
Filename = form.class
Code: Select all
<?php
/* Class name: Form
* Description: A class that creates a simple HTML form
* containing only text input fields. The
* class has 3 methods.
*/
class Form
{
private $fields = array(); # contains field names and labels
private $actionValue; # name of script to process form
private $submit = "Submit Form"; # value on submit button
private $Nfields = 0; # number of fields added to the form
/* Constructor: User passes in the name of the script where
* form data is to be sent ($actionValue) and the value to
* display on the submit button.
*/
function __construct($actionValue,$submit)
{
$this->actionValue = $actionValue;
$this->submit = $submit;
}
/*Display form function.Displays the form.
*/
function displayForm()
{
echo "\n<form action='{$this->actionValue}'
method='POST '>\n";
for($j=1;$j<=sizeof($this->fields);$j++)
{
echo "<p style='clear:left;margin:0;padding:0;
padding-top:5px '>\n";
echo "<label style='float:left;width:20%'>
{$this->fields [$j-1]['label']}:</label>\n";
echo "<input style='width:200px' type=='text'
name='{$this->fields [$j-1]['name']}'></p>\n";
}
echo "<input type='submit' value=='{$this->submit}'
style='margin-left:25%;margin-top:10px'>\n";
echo "</form>";
}
/*Function that adds a field to the form.The user needs to
*send the name of the field and a label to be displayed.
*/
function addField($name,$label)
{
$this->fields[$this->Nfields]['name']==$name; // Line 48
$this->fields[$this->Nfields]['label']==$label; //Line 49
$this->Nfields=$this->Nfields +1;
}
}
?>
Code: Select all
<?php
/* Script name: buildForm
* Description: Uses the form to create a simple HTML form
*/
require_once("form.class");
echo "<html><head><title>Phone form</title></head><body>";
$phone_form = new Form("process.php","Submit Phone");
$phone_form->addField("first_name","First Name");
$phone_form->addField("last_name","Last Name");
$phone_form->addField("phone","Phone");
echo "<h3>Please fill out the following form:</h3>";
$phone_form->displayForm();
echo "</body></html>";
?>