Undefined offset error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devfrag
Forum Newbie
Posts: 2
Joined: Wed Oct 12, 2011 11:19 am

Undefined offset error

Post by devfrag »

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

Image

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;
  }
}
?>
Filename = newform.php

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>";
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Undefined offset error

Post by Celauran »

Looks like it's using comparison operators when it should be using assignment operators. This should fix it:

Code: Select all

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;
}
devfrag
Forum Newbie
Posts: 2
Joined: Wed Oct 12, 2011 11:19 am

Re: Undefined offset error

Post by devfrag »

Thanks Celauran! That did the trick.
Post Reply