WillUK Posted November 25, 2010 Share Posted November 25, 2010 Hi I am in the process of converting to Object Oriented from Procedural. To cater for this I have built an admin_login function, contained within a class: 'siteFunctions'. However, I am having trouble pointing the admin form to the function correctly. Every time I click 'submit', the form does not process anything. It doesn't even 'think' about it i.e. show the egg timer.... I have built this script heaps of times using the procedural method, so I guess I am somehow doing something wrong with respect to referencing the action attribute of the form (due to my new approach). I am very new to OO so please go easy on me: I know the script isn't particularly advanced. I just want to get used to putting functions into classes, and then calling the code, before I move onto more advanced stuff. I have placed all of the files within the same folder in order to rule out driectory path issues. Here are the three scripts that I think are relevant (login, functionsClass, and the mysql connection script): Login $pageTitle = "Admin Login"; include("admin_header.php"); include_once("sitefunctions.php"); new siteFunctions(); echo '<div class="admin_main_body">'; <form action="<?php echo htmlentities($_SERVER["PHP_SELF"]);?>" method='post'> <input type="text" name="username" id="username" size="20"> <label>Username</label><br /> <input type="password" name="password" id="password" size="20"> <label>Password</label><br /> <input type="submit" name="submit" id="submit" value="submit"> </form> echo '<div>'; include("includes/admin_footer.php"); sitefunctions.php //$page = "admin_index.php"; class siteFunctions { var $message; function admin_login() { echo '<div class="admin_main_body">'; $message = NULL; if (isset($_POST['submit'])) { require_once ("mysql_connect.php"); if (empty($_POST['username'])) { $u = FALSE; $message .= '<p> Please enter your username </p>'; } else { $u = escape_data($_POST['username']); } if (empty($_POST['password'])) { $p = FALSE; $message .= '<p>You forgot to enter your password </p>'; } else { $p = escape_data($_POST['password']); } if ($u && $p) { // If everything's OK. $query = "SELECT * FROM admin WHERE username= ('$u') AND password=('$p')"; $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); $row = mysqli_fetch_array($result, MYSQLI_BOTH); if ($row) { session_start(); $_SESSION["admin_id"] = $row[0]; //header("$page"); //Redirects user to admin_index.php //header('location: "$page"'); header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "admin_index.php"); //echo '$_SESSION["admin_id"]'; } else { $message = '<p> The username and password combination are incorrect.</p>'; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } else { $message .= '<p>Please try again.</p>'; } } if (isset($message)) { echo '<font color="red">', $message, '</font>'; } //$adminLogin = 'admin_login'; } //Closes function } //Closes class Connection Script // This file contains the database access information. This file also establishes a connection to MySQL and selects the database. // Set the database access information as constants. DEFINE ('DB_USER', 'atkinson'); DEFINE ('DB_PASSWORD', 'XYZ111WA'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'practicesite'); if ($dbc = @($GLOBALS["___mysqli_ston"] = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD))) { // Make the connnection. if (!((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . constant('DB_NAME')))) { // If it can't select the database. // Handle the error. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not select the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); // Print a message to the user, include the footer, and kill the script. echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of mysql_select_db IF. } else { // If it couldn't connect to MySQL. // Print a message to the user, include the footer, and kill the script. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not connect to the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of $dbc IF. // Function for escaping and trimming form data. function escape_data ($data) { global $dbc; if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } return mysqli_real_escape_string( $dbc, trim ($data)); } // End of escape_data() function. Any help would be appreciated. Cheers Will Quote Link to comment https://forums.phpfreaks.com/topic/219753-problems-with-referencing-form-action-attribute-to-a-function-within-a-class/ Share on other sites More sharing options...
efficacious Posted November 25, 2010 Share Posted November 25, 2010 this could be a stupid question but, How are you passing the variables to the function? I see the action attribute in the form tag but it just submits to "self" I can't find any code the passes the variables to the function Nvm I think i see what your doing.. Try this.. put in a script to alert(""); the values of the variables after you've submitted just to check if they are there. Quote Link to comment https://forums.phpfreaks.com/topic/219753-problems-with-referencing-form-action-attribute-to-a-function-within-a-class/#findComment-1139235 Share on other sites More sharing options...
Anti-Moronic Posted November 25, 2010 Share Posted November 25, 2010 Hi Will, You need to first use your 'method' which is the function within your 'object' which is the class. When you do this: new siteFunctions(); That does nothing but create a new object. You then have to do something with that object (others - I'm simplifying, don't correct me). Ideally, it would look like so: $siteFunctions = new siteFunctions(); $siteFunctions->admin_login(); Now that will work as long as your code isn't to blame. Try that first and come back if it still doesn't work. - if the page isn't even redirecting after submit, there is something wrong with your html, not php, but I can't see any errors. Now, one important point about OOP. I used to do this, one class with lots of functions and think it was OOP but it isn't really. You have to understand that true OOP will use more than one object/class, and in most cases they use lots of small objects which interact with each other. You should then try to categorize your functions/methods and break everything down into something more simple and manageable. It seems simple to put everything in one file, one function, one class - but in the end, that is actually the hard way to do it. Lots of beginners do that and don't know how much harder they're making it on themselves. What I would suggest is you download Zend and read the quickstart guide for it. Set up a test app and you will learn OOP in far less time! Then as you do that, build your own test application with the fundamentals you are learning from Zend Framework - which is best practice OOP. Hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/219753-problems-with-referencing-form-action-attribute-to-a-function-within-a-class/#findComment-1139237 Share on other sites More sharing options...
Anti-Moronic Posted November 25, 2010 Share Posted November 25, 2010 this could be a stupid question but, How are you passing the variables to the function? I see the action attribute in the form tag but it just submits to "self" I can't find any code the passes the variables to the function The variable is in the $_POST - within the function. It's a superglobal so no need to pass it as a parameter to the function. I *think* the problem is that the function is not even being called. Only an object is being created, which means we can only guarantee the __constructor() is being called (which this object doesn't have). Quote Link to comment https://forums.phpfreaks.com/topic/219753-problems-with-referencing-form-action-attribute-to-a-function-within-a-class/#findComment-1139238 Share on other sites More sharing options...
efficacious Posted November 25, 2010 Share Posted November 25, 2010 this could be a stupid question but, How are you passing the variables to the function? I see the action attribute in the form tag but it just submits to "self" I can't find any code the passes the variables to the function The variable is in the $_POST - within the function. It's a superglobal so no need to pass it as a parameter to the function. I *think* the problem is that the function is not even being called. Only an object is being created, which means we can only guarantee the __constructor() is being called (which this object doesn't have). Yea I noticed the $_POST after i posted 8/ But I see what you've said and you are correct. I would have gotten there but I haven't done much OOP with php Quote Link to comment https://forums.phpfreaks.com/topic/219753-problems-with-referencing-form-action-attribute-to-a-function-within-a-class/#findComment-1139240 Share on other sites More sharing options...
Anti-Moronic Posted November 25, 2010 Share Posted November 25, 2010 this could be a stupid question but, How are you passing the variables to the function? I see the action attribute in the form tag but it just submits to "self" I can't find any code the passes the variables to the function The variable is in the $_POST - within the function. It's a superglobal so no need to pass it as a parameter to the function. I *think* the problem is that the function is not even being called. Only an object is being created, which means we can only guarantee the __constructor() is being called (which this object doesn't have). Yea I noticed the $_POST after i posted 8/ But I see what you've said and you are correct. I would have gotten there but I haven't done much OOP with php Simple mistake, I think I've made about 20-30 of them today I'd definitely advise you look into OOP more. Personally, I do think it is the *only* way to be an effective php programmer and protect your long-term demand (unless you are doing for hobby, of course). Quote Link to comment https://forums.phpfreaks.com/topic/219753-problems-with-referencing-form-action-attribute-to-a-function-within-a-class/#findComment-1139253 Share on other sites More sharing options...
WillUK Posted November 25, 2010 Author Share Posted November 25, 2010 Hi Guys Thanks for your help. I am working on building classes, breaking them down and modulating them. But the way I see it: I need to get one thing working first lol But notice that I've altered the name of the class itself to 'login', which is certainly more modular than siteFunctions.... Anyway - I've instantiated the class, and called the admin_login method as suggested: but it still isn't working properly... However, the error I am now getting suggests that there is a 'parse error' on line 4 of sitefunctions.php. However - maybe i'm blind - but I can't see anything wrong with the syntax... Here are my updated scripts: Admin Login Form (index.php) $pageTitle = "Admin Login"; include("admin_header.php"); include_once("sitefunctions.php"); $login = new $login(); //Changed the name of the class to be more modular. Instantiates the login class. $login->admin_login(); //Calls the admin login function. echo '<div class="admin_main_body">'; [/php> <form action="<?php echo htmlentities($_SERVER["PHP_SELF"]); ?>" method='post'> <!--echo $_SERVER["PHP_SELF"];--> <input type="text" name="username" id="username" size="20"> <label>Username</label><br /> <input type="password" name="password" id="password" size="20"> <label>Password</label><br /> <input type="submit" name="submit" id="submit" value="submit"> </form> [code=php:0] echo '<div>'; include("admin_footer.php"); Site Functions (sitefunctions.php) class login { var $message = NULL; var $username = $_POST['username']; //Parse error here?!!!!! var $password = $_POST['password']; function admin_login() { echo '<div class="admin_main_body">'; $message = NULL; if (isset($_POST['submit'])) { require_once ("mysql_connect.php"); if (empty($username)) { $username = FALSE; $message .= '<p> Please enter your username </p>'; } else { $username = TRUE; $username = escape_data($username); } if (empty($password)) { $password = FALSE; $message .= '<p>You forgot to enter your password </p>'; } else { $password = TRUE; $password = escape_data($password); } if ($username && $password) { // If everything's OK. $query = "SELECT * FROM admin WHERE username= ('$username') AND password=('$password')"; $result = @mysqli_query($GLOBALS["___mysqli_ston"], $query); $row = mysqli_fetch_array($result, MYSQLI_BOTH); if ($row) { session_start(); $_SESSION["admin_id"] = $row[0]; //header("$page"); //Redirects user to admin_index.php //header('location: "$page"'); header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "admin_index.php"); } else { $message = '<p> The username and password combination are incorrect.</p>'; } ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res); } else { $message .= '<p>Please try again.</p>'; } } if (isset($message)) { echo '<font color="red">', $message, '</font>'; } //$adminLogin = 'admin_login'; } //Closes function } //Closes class Connection Script (mysql_connect.php) // This file contains the database access information. This file also establishes a connection to MySQL and selects the database. // Set the database access information as constants. DEFINE ('DB_USER', 'atkinson'); DEFINE ('DB_PASSWORD', 'XYZ111WA'); DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_NAME', 'practicesite'); //include ('config.inc'); if ($dbc = @($GLOBALS["___mysqli_ston"] = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD))) { // Make the connnection. if (!((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE " . constant('DB_NAME')))) { // If it can't select the database. // Handle the error. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not select the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); // Print a message to the user, include the footer, and kill the script. echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of mysql_select_db IF. } else { // If it couldn't connect to MySQL. // Print a message to the user, include the footer, and kill the script. my_error_handler (((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_errno($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)), 'Could not connect to the database: ' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false))); echo '<p><font color="red">The site is currently experiencing technical difficulties. We apologize for any inconvenience.</font></p>'; include_once ('includes/footer.php'); exit(); } // End of $dbc IF. // Function for escaping and trimming form data. function escape_data ($data) { global $dbc; if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } return mysqli_real_escape_string( $dbc, trim ($data)); } // End of escape_data() function. Sometimes programming gets so frustrating! Please tell me I'm not a million miles away with this, otherwise I may just throw my laptop out of the office window! Thanks again to you both. Quote Link to comment https://forums.phpfreaks.com/topic/219753-problems-with-referencing-form-action-attribute-to-a-function-within-a-class/#findComment-1139566 Share on other sites More sharing options...
WillUK Posted November 27, 2010 Author Share Posted November 27, 2010 I've solved it! Although I'm not entirely sure why the amendment was necessary.... I made the database connection ($dbc) global within the mysql_connect script. Then everything seemed to work fine. When I was using the procedural approach this was never an issue, thus using the procedural approach ($dbc) did not have to be global. When I was trying to run the script prior to setting it as global I was getting the error "parameter 1 expected to be mysqli, null given" (or something like that). Confusing! That's programming I guess: not my favourite part of the job! Anyway, thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/219753-problems-with-referencing-form-action-attribute-to-a-function-within-a-class/#findComment-1140375 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.