Cobby Posted June 16, 2007 Share Posted June 16, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/ Share on other sites More sharing options...
emehrkay Posted June 16, 2007 Share Posted June 16, 2007 class login{ var $user = ''; var $pass = ''; function login($user, $pass){ $this->user = $user; $this->pass = $pass; } } to use it do $login = new login($_POST['user'], $_POST['pass']); Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-275665 Share on other sites More sharing options...
Cobby Posted June 16, 2007 Author Share Posted June 16, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-275673 Share on other sites More sharing options...
Cobby Posted June 16, 2007 Author Share Posted June 16, 2007 BUMP I have been playing around for a few hours, but I gave up, can anyone tell me how I would echo the username input in a different function, but still in the same class? Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-275817 Share on other sites More sharing options...
Cobby Posted June 16, 2007 Author Share Posted June 16, 2007 I feel rude, but this is such a simple question. How do I echo the string from another variable? Is anyone going to help? Cobby Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-276040 Share on other sites More sharing options...
Pastulio Posted June 16, 2007 Share Posted June 16, 2007 I think it's also "$this->user" and etc... Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-276050 Share on other sites More sharing options...
Cobby Posted June 16, 2007 Author Share Posted June 16, 2007 But I need to pass $_POST['username'], $_POST['password'] through the login() function first. Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-276059 Share on other sites More sharing options...
Pastulio Posted June 16, 2007 Share Posted June 16, 2007 Yes but you store them in the "this->" varibles? Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-276062 Share on other sites More sharing options...
Cobby Posted June 16, 2007 Author Share Posted June 16, 2007 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-276066 Share on other sites More sharing options...
Pastulio Posted June 17, 2007 Share Posted June 17, 2007 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-276089 Share on other sites More sharing options...
Cobby Posted June 17, 2007 Author Share Posted June 17, 2007 Nah that didn't work. Somewhere in there, you need to have login($_POST['username']. But its just where? Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-276120 Share on other sites More sharing options...
emehrkay Posted June 17, 2007 Share Posted June 17, 2007 reread my first example put the class in its own file, call it class.login.php in your form processing page do require 'class.login.php' then call it using $login = new login($_POST['user'], $_POST['pass']); Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-276132 Share on other sites More sharing options...
Cobby Posted June 17, 2007 Author Share Posted June 17, 2007 Sounds good, will do But just for the sake of learning, how would I have done it within the same class? Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-276134 Share on other sites More sharing options...
emehrkay Posted June 17, 2007 Share Posted June 17, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/55806-solved-using-_post-in-oop/#findComment-276137 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.