Referring to Super Global Array in Classes

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
User avatar
phppage
Forum Contributor
Posts: 126
Joined: Mon Apr 24, 2006 1:47 pm
Location: West Yorkshire, UK

Referring to Super Global Array in Classes

Post by phppage »

I seem to be having some issues when referring to post data array within a class. I am using the class below to echo out inputs within a HTML form. I am trying to create a sticky input however the class dose not see the POST data after a submit. I take it I don't need to declare it as global within the class as it is a super global array?

Code: Select all

class textinput {
 var $name;
 var $label;
 var $size;
 var $maxsize;
 var $value;
 var $nobr;

 
 function display( ) {
  $this->findvalue( );
  $this->findmaxsize( );
  $this->arrange( );
 }
 
 function arrange( ) {
  echo "<label for=\"" . $this->name . "\">" . $this->label . " </label><input type=\"text\" name=\"" . $this->label . "\" size=\"" . $this->size . "\" maxlength=\"" . $this->maxsize . "\" value=\"" . $this->value . "\" />";
  if (!isset($this->nobr)) { echo "<br>"; }
 }

 function findvalue( ) {
   if (isset($_POST[$this->name])) { $this->value = $_POST[$this->name]; } else {
       if(!isset($this->value)) { $this->value = "";  } }
   }
 function findmaxsize( ) {
   if (!isset($this->maxsize)) { $this->maxsize = $this->size; }
 
 }
}
Many Thanks :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your class doesn't really seem to set any of the properties other than "value" and "maxsize." Where's "name," "label," "size" and "nobr" set?
User avatar
phppage
Forum Contributor
Posts: 126
Joined: Mon Apr 24, 2006 1:47 pm
Location: West Yorkshire, UK

Post by phppage »

feyd wrote:Your class doesn't really seem to set any of the properties other than "value" and "maxsize." Where's "name," "label," "size" and "nobr" set?
Sorry Feyd, This well could be a complete hash. Have only being playing about with classes for the last couple of hours. I think I'm setting them when I call them. See below

Code: Select all

$email = new textinput;
$email->name = "email";
$email->label = "Email Address: ";
$email->size = "20";
$email->display( );
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I believe if you check the HTML that this generates you'll find that the form element's name isn't "email" but "Email Address: ".
User avatar
phppage
Forum Contributor
Posts: 126
Joined: Mon Apr 24, 2006 1:47 pm
Location: West Yorkshire, UK

Post by phppage »

Doh! :lol:
Many thanks Feyd. Its always the way when your trying some new. When it goes wrong, always over look the simplest things.

Cheers :)
Post Reply