filthy_wizard
filthy_wizard

Reputation: 740

Not getting POST data from within class

I am new to PHP and I have a project to complete as part of a bigger project to create a website for college.

I am trying to use the POST method in an HTML page to send data to a class named user.

This seems simple enough, but no matter what I do, I cannot get the data I enter into the input box return, I keep getting "not set" returned.

Form HTML

<form method="post" action="user.php">
<table cellpadding="5">
 <tr>
  <td><label for="firstname">Enter Firstname:</label></td>
  <td><input type="text" id="firstname" name="firstname" /><br /></td>
 </tr>  
 <tr>
  <td><label for="surname">Enter Surname:</label></td>
  <td><input type="text"   id="surname" name="surname" /><br /></td>
 </tr>
<tr>
  <td> <label for="email">Email Address:</label></td>
  <td><input type="text" id="email" name="email" /><br /></td>
 </tr>
<tr>
  <td><label for="password">Enter Password:</label></td>
  <td><input type="password" id="password" name="password" /><br /></td>
 </tr>
<tr>
  <td><label for="renter">Re-Enter Password:</label></td>
  <td><input type="password" id="renter" name="renter" /><br /></td>
 </tr>
<tr>
  <td><label for="address1">Address Line 1:</label></td>
  <td><input type="text" id="address1" name="address1" size="35"/><br /></td>
 </tr>
<tr>
  <td><label for="address2">Address Line 2:</label></td><td><input type="text"    id="address2" name="address2" size="35"/><br /></td>
 </tr>
<tr>
  <td><label for="city">City:</label></td>
  <td><input type="text" id="city" name="city" /><br /></td>
 </tr>
<tr>
  <td><label for="postcode">Postcode:</label></td>
  <td><input type="text" id="postcode"     name="postcode" /><br /></td>
 </tr>
<tr>
  <td><label for="country">Country:</label></td>
  <td><input type="text" id="country" name="country" /><br /></td>
 </tr>
 <tr>
  <td><input type="submit" value="Submit Details" name="submit" class="button1"/></td>
</table>
</form>

I am trying to get the input name="firstname" printed on the page first; then I will begin to worry about everything else. Below is my class and some more HTML I use to print the firstname.

Result HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head></head
<body>
<p>thank you! user has been created</p>
<?php

$newuser = new user();
$theuser = $newuser->getfirstname();

echo $theuser;

?>
</body>
</html>

user CLASS

<?php

class user
{
    public $email;
    public $firstname;
    public $lastname;
    public $connection;

    function _construct()
    {
        $this->firstname= $_POST['firstname'];
        $this->lastname = $_POST['lastname'];
        $this->email=$_POST['email'];
    }

    public function  getfirstname()
    {
        if(isset($firstname))
        {
            return $this->firstname;
        }
        else
        {
            return "not set";
        } 
    }
}

?>

Upvotes: 0

Views: 3405

Answers (1)

Jared Farrish
Jared Farrish

Reputation: 49208

As the commenters have mentioned, your code should work if you change:

if(isset($firstname)) // This isn't Java...

to:

if(isset($this->firstname)) // ... you need $this->

As well as:

_construct() // Not one _

to:

__construct() // But two __

See this:

<?php

class user
{
    public $email;
    public $firstname;
    public $lastname;
    public $connection;

    function __construct()
    {
        $this->firstname = $_POST['firstname'];
        $this->lastname = $_POST['lastname'];
        $this->email = $_POST['email'];
    }

    public function getfirstname()
    {
        print_r($this);
        if(isset($this->firstname))
        {
            return $this->firstname;
        }
        else
        {
            return "not set";
        } 
    }

}

http://jfcoder.com/test/oopuser.php

EDIT

Note as well, using tables for layout is considered an old, poor approach. For instance, below is an approach using a UL and CSS. This works great in FF and Chrome, but fails miserably in IE (I think due to some of the selectors). However, this tutorial can explain the overall approach:

http://www.alistapart.com/articles/prettyaccessibleforms

CSS

#register {
    width: 28em;
    background: #ddd;
}
#register ul,
#register li {
    margin: 0;
    padding: 0;
}
#register li {
    margin: 0 0 1px;
    list-style-type: none;
    background: whiteSmoke;
    display: auto;
}
#register li:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}
#register label {
    float: left;
    width: 8em;
    clear: both;
}
#register input[type=text],
#register input[type=password] {
    width: 12em;
}
#register  input[type=text].address {
    width: 100%;
}
#register label,
#register .data,
#register-submit {
    padding: .5em;
    margin: 0;
}
#register .data {
    float: left;
    margin: 0;
    width: 18em;
}
#register-submit {
    clear: both;
    text-align: right;
}
#register hr {
    height: 1px;
    background: #bbb;
    border: 0;
}

HTML

<form id="register" method="post" action="oopuser.php">
 <h1>Registration Form</h1>
 <ul>
  <li>
   <label for="firstname">Enter Firstname:</label>
   <span class="data">
    <input type="text" id="firstname" name="firstname" />
   </span>
  </li>
  <li>
   <label for="surname">Enter Surname:</label>
   <span class="data">
    <input type="text" id="surname" name="surname" />
   </span>
  </li>
  <li>
   <label for="email">Email Address:</label>
   <span class="data">
    <input type="text" id="email" name="email" />
   </span>
  </li>
  <li>
   <label for="password">Enter Password:</label>
   <span class="data">
    <input type="password" id="password" name="password" />
   </span>
  </li>
  <li>
   <label for="renter">Re-Enter Password:</label>
   <span class="data">
    <input type="password" id="renter" name="renter" />
   </span>
  </li>
  <li>
   <label for="address1">Address Line 1:</label>
   <span class="data">
    <input type="text" id="address1" name="address1" class="address"/>
   </span>
  </li>
  <li>
   <label for="address2">Address Line 2:</label>
   <span class="data">
    <input type="text" id="address2" name="address2" class="address"/>
   </span>
  </li>
  <li>
   <label for="city">City:</label>
   <span class="data">
    <input type="text" id="city" name="city" />
   </span>
  </li>
  <li>
   <label for="postcode">Postcode:</label>
   <span class="data">
    <input type="text" id="postcode" name="postcode" />
   </span>
  </li>
  <li>
   <label for="country">Country:</label>
    <span class="data">
    <input type="text" id="country" name="country" />
    </span>
  </li>
  <li id="register-submit">
   <input type="submit" value="Submit Details" name="submit"/>
   <input type="reset" value="Reset" name="reset"/>
  </li>
 </ul>
</form>

http://jsfiddle.net/JWUC9/

Upvotes: 2

Related Questions