Current location: Hot Scripts Forums » Programming Languages » PHP » My options won't show, but there are no error msg


My options won't show, but there are no error msg

Reply
  #1 (permalink)  
Old 07-27-09, 06:36 PM
Angie91 Angie91 is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
My options won't show, but there are no error msg

Hi all,
I'm new to PHP and I'm trying to learn from a book/examples. So far everything else has worked out just fine, but I can't seem to get this script correct. I've copied the script off the book, and I've gone over it several times, but I can't seem to get it right....

I'm making a simple form selection list. The form comes up, and there are the correct number of choices. The only problem is that you can't actually see the choices. Which is neatly explained by the source code. Only problem is that I can't seem to figure out what I'm doing wrong..

From source code:
HTML Code:
<option value=""></option>
script:
PHP Code:

$fruits->setOption(new Option('Oranges''Oranges')); 

PHP Code:

public function _construct($value=''$label='') {
        
parent::_construct();
        
$this->setValue($value);
        
$this->setLabel($label);
    }
    
    
//method: output HTML for <option> elements
    
public function render() {
        echo 
"<option value=\"" $this->getValue() . "\">" $this->getLabel() . "</option>\n";
    } 
PHP Code:

//method: output HTML code for <select> element
    
public function render() {
        echo 
$this->getLabel() . ": <br />\n";
        echo 
"<select name=\"" $this->getName() . "\">\n";
        foreach (
$this->getOptions() as $opt) {
            echo 
$opt->render();
        }
        echo 
"</select>";
    } 
These are the parts of the script I find relevant for the error. The getLabel and getName parts from the last function works, but not the getOptions.... If this is too little of the script to think of anything, just say and i'll give you the remaining part...

Edit:
after i press submit, I get this output:

array(4) { ["fruit_sel"]=> string(0) "" ["metal_sel"]=> string(0) "" ["animal_sel"]=> string(0) "" ["submit"]=> string(6) "Submit" }

Last edited by Angie91; 07-27-09 at 06:54 PM.
Reply With Quote
  #2 (permalink)  
Old 07-28-09, 05:21 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Place this at the very top of your script and see if that gives you any hints:
PHP Code:

error_reporting(E_ALL);
ini_set('display_errors''1'); 
Reply With Quote
  #3 (permalink)  
Old 07-28-09, 07:03 AM
Angie91 Angie91 is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
No change. I still get blank listings... :/
Reply With Quote
  #4 (permalink)  
Old 07-28-09, 01:57 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Could you post the whole file?
Reply With Quote
  #5 (permalink)  
Old 07-29-09, 08:53 AM
Angie91 Angie91 is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

<?php  

//class definition
class Element {
    private 
$name;
    private 
$value;
    private 
$label;
    
    
//constructor
    
public function _construct() {
    }
    
    
//method: set element 'name'
    
public function setName($name) {
        
$this->name $name;
    }
    
    
// method: get element 'name'
    
public function getName() {
        return 
$this->name;
    }
    
    
//method: set elment value
    
public function setValue($value) {
        
$this->value $value;
    }
    
    
//method: get element value
    
public function getValue() {
        return 
$this->value;
    }
    
    
//mehod: set English-laguage element label
    
public function setLabel($label) {
        
$this->label $label;
    }
    
    
//method: get element label
    
public function getLabel() {
        return 
$this->label;
    }
}
?>
<?php 
//child class definition
class Option extends Element {
    
    
// comstructor
    
public function _construct($value=''$label='') {
        
parent::_construct();
        
$this->setValue($value);
        
$this->setLabel($label);
    }
    
    
//method: output HTML for <option> elements
    
public function render() {
        echo 
"<option value=\"" $this->getValue() . "\">" $this->getLabel() . "</option>\n";
    }
}
?>

<?php  
//child class definition
class Select extends Element {

    protected 
$options;
    
    
//constructor
    
public function _construct() {
        
parent::_construct ();
        
$this->options = array();
    }
    
    
// method: add an option to the list
    
public function setOption($option) {
        
$this->options[] = $option;
    }
    
    
//method: return all options for the list as array
    
private function getOptions() {
        return (array)
$this->options;
    }
    
    
//method: output HTML code for <select> element
    
public function render() {
        echo 
$this->getLabel() . ": <br />\n";
        echo 
"<select name=\"" $this->getName() . "\">\n";
        foreach (
$this->getOptions() as $opt) {
            echo 
$opt->render();
        }
        echo 
"</select>";
    }
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Project 5-3: Generating Form Selection Lists</title>
    </head>
    <body>
        <h2>Project 5-3: Generating Form Selection Lists</h2>
<?php  
    
// if form not yet submitted
    // display form
    
if(!isset($_POST['submit'])) {
?>
    <form method="post" action="select.php">
        <?php
        
        
//generate selection list #1
        
$fruits = new Select();
        
$fruits->setLabel('Fruits');
        
$fruits->setName('fruit_sel');
        
$fruits->setOption(new Option('Oranges''Oranges'));
        
$fruits->setOption(new Option('Apples''Apples'));
        
$fruits->setOption(new Option('Pears''Pears'));
        
$fruits->setOption(new Option('Pineapples''Pineapples'));
        
$fruits->render();
        
?>
        
        <p />
        
        <?php
        
// genereate selection list #2
        
$metals = new Select();
        
$metals->setLabel('Metals');
        
$metals->setName('metal_sel');
        
$metals->setOption(new Option('Gold''Gold'));
        
$metals->setOption(new Option('Silver''Silver'));
        
$metals->setOption(new Option('Iron''Iron'));
        
$metals->setOption(new Option('Platinum''Platinum'));
        
$metals->render();
        
?>
        
        <p />
        
        <?php
        
// generate selection list #3
        
$animals = new Select();
        
$animals->setLabel('Animals');
        
$animals->setName('animal_sel');
        
$animals->setOption(new Option('Polar Bear''Polar Bear'));
        
$animals->setOption(new Option('Penguin''Penguin'));
        
$animals->setOption(new Option('Kitten''Kitten'));
        
$animals->setOption(new Option('Hyena''Hyena'));
        
$animals->setOption(new Option('Sheep''Sheep'));
        
$animals->render();
        
?>
        <p />
        <input type="submit" name="submit" value="Submit" />
    </form>
<?php
    
// if form submitted
    // process form input
    
} else {
        
var_dump($_POST);
    }
?>
    </body>
</html>
Reply With Quote
  #6 (permalink)  
Old 07-29-09, 12:41 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
This is the code that is dumping out the form input. It's telling you that the inputs have been received and letting you know what values were sent from the browser.

PHP Code:

<?php 

    
// if form submitted 
    // process form input 
    
} else { 
        
var_dump($_POST); 
    } 
?>
I'm guessing based on the var_dump output that you may have hit 'Submit' before you selected any options.

Try selecting some options, then click Submit.
Reply With Quote
  #7 (permalink)  
Old 07-29-09, 12:55 PM
Angie91 Angie91 is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
The problem is that you can't actually see any options, it's just blank lists. No matter what you try to select, you get the outcome stated last in my initial post.
Reply With Quote
  #8 (permalink)  
Old 07-29-09, 01:09 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Your constructor is never being called. It should be __construct, with 2 underscores at the beginning.

Also, if your function does not require a constructor, like your first one, you can just leave it out completely.
Reply With Quote
  #9 (permalink)  
Old 07-29-09, 02:07 PM
Angie91 Angie91 is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
wey, now the lists are working =D Not so easy to spot if it's one or two underscores ;P Still get the same error when I submit tho.

PHP Code:

array(4) { ["fruit_sel"]=>  string(5"Pears" ["metal_sel"]=>  string(8"Platinum" ["animal_sel"]=>  string(6"Kitten" ["submit"]=>  string(6"Submit" 
Reply With Quote
  #10 (permalink)  
Old 07-29-09, 03:17 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
This isn't an error - this is actually what you want to see.

Instead of the var_dump, you can put in the processing for the selections, for example:

PHP Code:

echo 'Animal selection: '.$_POST['animal_sel'].'<br />'
Reply With Quote
Reply

Bookmarks

Tags
error, php


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
show tables question maklooby PHP 1 04-19-09 05:47 PM
slide show question maklooby JavaScript 4 03-13-09 07:03 PM
Need script to show matting/framing options for artwork Inkmaker1948 Script Requests 3 03-07-08 08:46 AM
change options of select based on selection in other select nassau JavaScript 3 08-31-06 08:00 AM


All times are GMT -5. The time now is 07:55 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.