为什么这个PHP对象代码不起作用?

时间:2014-11-26 16:11:06

标签: php

我正在阅读傻瓜的“PHP,MySQL,Javascript”对象章节,我无法让他们的一个例子起作用,我无法弄清楚代码有什么问题。此代码直接从书中复制:

创建课程

<?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;
    $this->fields[$this->Nfields][‘label’] = $label;
    $this->Nfields = $this->Nfields + 1;
} }
?>

创建对象和HTML

<?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>”;
?>

1 个答案:

答案 0 :(得分:5)

此文件中包含无效引号:

更改为"

'

PHP应该在这里抛出很多错误。