Jump to content

[SOLVED] Using $_POST in OOP


Cobby

Recommended Posts

Hello all,

 

I am coding a CMS using OOP (Object Orientated Programming). I have just started, and I am half way through the user login/authentication area.

 

How do I get data from a HTML form in a class?

 

I have a regular HTML some like:

<form action="post" method="checklogin.php">
<input type="text" name="username" />
<input type="text" name="password" />
</form>

 

 

But thats all fine. Here is what I have done so far on checklogin.php:

 

<?

require_once ('../includes/dbconnect.php');

$dbconnect = new dbconnect();

class login
{

    var $username = $_POST['username'];
    var $password = $_POST['password'];

    function login()
    {
        if (isset($_POST['submit']))
        {

        }
    }
}

?>

 

I get a parse error on line 10.

Line 7 is: var $username = $_POST['username'];

 

If I change them both to a regular srting like

 

var $username = "Cobby";
var $password= "Cobby's Password";

 

It works fine, so I guess I am doing something when getting the data from the HTML form.

 

How do I get data from a HTML form in a class?

 

Cheers,

Cobby

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/
Share on other sites

Thanks for the reply... :)

 

But how do I call on that from within another function?

What would I do if I wanted to simply echo the username (from within a new function, but same class)?

 

It seams like such I simple question, but I just cant put my finger on it? ???

 

OOPS (no I dont mean object orientated), forgot about the OOP Child Board, Moderators feel free to move my thread.

Yes but you store them in the "this->" varibles?

 

Sorry, you've lost me.

 

class login{

 

    var $username = "";

    var $password = "";

 

    function login($username, $password){

   

    if(isset($_POST['submit'])){

$this->username = $username;

$this->password = $password;

}

 

 

    }

   

    function querydb(){

 

$login->login($_POST['username']); //just doing username for the time being, ill add password when this gets working.

echo $login;

 

}

}

I'm not the pro in OOP but I think you just need to do the same thing again.

 

class login{

 

    var $username = "";

    var $password = "";

 

    function login($username, $password, $submit){

     

      if(isset($this->submit)){

      $this->username = $username;

      $this->password = $password;

      }

 

     

    }

 

    function querydb($username){

 

  $login->login($this->username); //just doing username for the time being, ill add password when this gets working.

  echo $login;

 

  }

}

Sounds good, will do :)

 

But just for the sake of learning, how would I have done it within the same class?

 

im not too sure if it is possible

 

but for kicks try

 

class test{

function test(){

print_r($_POST);

}}

 

then on a page that has post varialbes do

 

$x = new test(); and see what happens

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.