Jump to content
Larry Ullman's Book Forums

Edward

Members
  • Posts

    1115
  • Joined

  • Last visited

  • Days Won

    27

Everything posted by Edward

  1. The registration error array is easier it's saves you having to write that long if conditional then you can pass validation with a simple check to see whether you errors array is empty.
  2. Why bother with the agreement check box, just put in a notice before the submit button of the form quoting "if you click the submit button below you are agreeing to our terms". I done this and its save the extra validation and time and looks better than that ugly checkbox.
  3. Well the database has to check there is at least one record to process the next section. You have to check for something otherwise how can you process next section of script
  4. Edward

    My Project Diary

    Tuesday, August 07, 2012 Worked on my activate php script today, which is a script where the user is asked to enter the username they would like and set there password. This was the first form i have work on with values passed to to the scrip in the URL so i was required to check the $_GET array. Now the problem is in this little form there is also some validation to check if the user had entered a validate username, password and also the confirmation password matched the first one typed in. So what will happen to our $_GET values if the form is submitted on this page, they will be lost yes. So i had to add in two hidden values for the email and activation code in the form, so now at the top of my php script i also have to check for $_POST values being passed to it, and set the values for the form in these as well if the form has to be submitted again. I have a slight problem now trying to take information from a single row using the oop code as object's, i am using fetch_array() but the first_name which i require from the database is not coming out correctly so its something i need to work on. One other thing i have noticed is that since changed to OOP mysqli connections, my custom error handler seems to be no longer working correctly, so its another thing i will have to look into.
  5. Edward

    My Project Diary

    Monday, August 06, 2012 From yesterday DATETIME was replace with DATE for saving the birthday, i used 2012 as a leap year for the year. I have managed server hosting with rackspace, i tried using the php mail() function but it failed to work, so i was instructed to join SendGrid.com SMTP to send my email through. I have made an account with SendGrid.com and Rackspace that are partnered with them have automatically sent my account up so that now the php mail() function will send emails to Postfix which will then relay them to SendGrid as end recipient. Ive just tried my mail function in the registration script in the point where you are sent an activation code, i clicked it, and just in 1 second i received a message to my Samsung Galaxy Note indicating that i had received an email. We get to use 40,000 emails with SendGrid Free per month as Rackspace has partnered up with them. By the way i should mention the reason for using SendGrid.com, most email providers like Gmail, Hotmail etc will flag messages as spam or even blacklist hosts from sending emails so they will be rejected. So if we send our emails via SendGrid.com they will pass through successfully. I have been quite lucky as Rackspace configured the setting for SendGrid, i thought i would have to add in another API, if you are interested to see you can check this out, this is some documentation on how to send emails through php with SendGrid http://docs.sendgrid...ple-using-smtp/. This has been not such a smooth day for me at the beginning, i run into an error problem of having to enter data from my registration form into two tables, an address and user table. I could see that there would be a problem adding two queries at a time or even a multi query, what happens if we have a system crash for example, we may lose have of the info. I wasn't sure what do exactly so i asked Larry early and he kindly recommended i use transactions for this. I dealt with the problem by adding a $mysqli->autocommit(FALSE) at the top of my script this would turn off autocommit on queries. I later used $mysqli->rollback() to delete in previous entered transactions if there was an error and also use the $mysqli->commit() when all records were fine to enter. That leaves my registration form almost finished i will be working on mail function tomorrow and working on the activation page, where a user can enter his username and password for the web site to be fully registered.
  6. Edward

    My Project Diary

    Thank you very much, its fun using all the stuff from the books i have read by you. Sunday, August 05, 2012 Today i have changed all mysqli connections into the object orientated versions all of which is now running rather smoothly. I run into an issue with storing birthday in the database, i wondered if i should have two columns for the month and day but my instincts told me this was not good practice. I know that SQL DATETIME variable needs to store the date as well as time, so Ive decided to store birthday in the database as DATETIME and just include the year 2012 which is a leap year and so February the 29th would be accepted if someone was to have a birthday on that day. I run into I would say a little Parse error which took me about 20 minutes to find. It was my mistake, i went in confident and changed a lot of code at one time, later finding an indication on Dreamweaver right at the bottom of my 300 line script the Parse error. I was like, 'Hey What's up Dreamweaver how could you let one slip like that? I wasn't able to find the error, later i start commenting out code until i found it, then i remember i did read about the code commenting in Larry's book, i won't forget that one again. And now i know by the hard way that its better to check each part as i go. Just done my first oop prepared statement but one thing i wasn't sure about is how can you get a date in format yyyy-mm-dd 00:00:00 into MySQL, which letter do you use, d s i, i tried d it didn't work, so i tried s for string to pass it and it worked. Yeah! Inspiration i got from Larry's code: Before include ('includes/header.html'); include ('includes/footer.html'); After include (HEADER); include (FOOTER); Yes you tell me which is better, well thanks to Larry he pointed me in the right direction. Whoops Ive just realized i can use a DATE variable in SQL, okay i will go to fix that now, be back tomorrow with more updates.
  7. Edward

    My Project Diary

    Here are some of my form fields just to show how they look with the new functions, these cut the code down substantially: <div class="row <?php Form_Functions::createErrorClass('email', $reg_errors); ?>"> <label for="email">Email Address:</label> <?php Form_Functions::createFormInput('email','email', array('size'=>20, 'maxlengh'=>50)); ?> </div> <div class="row inline <?php Form_Functions::createErrorClass('first_name', $reg_errors); ?>"> <label for="first_name">First Name:</label> <?php Form_Functions::createFormInput('first_name','text', array('size'=>20, 'maxlengh'=>50)); ?> </div> <div class="row inline <?php Form_Functions::createErrorClass('last_name', $reg_errors); ?>"> <label for="last_name">Last Name:</label> <?php Form_Functions::createFormInput('last_name','text', array('size'=>20, 'maxlengh'=>50)); ?> </div> I noticed though some slight announce, i think when i added an Id to the email tag, when I started to enter the email and then clicked off it the input email box went red indicating and error. Looks like some kind of HTML5 validation, it came up on my mobile device and also my mozilla browser.
  8. Edward

    My Project Diary

    I have added an error summary above my Registration form which prints out all the errors held in the errors array. I also have added css error classes into all of the forum inputs which were not filled in. Rather than using functions i decided to make a Form_Functions Class with static methods to use. Here is my class. <?php /** * Class to define commonly used form functions * */ class Form_Functions { /** * This method generates and form row error class. * It takes two arguments: * - The name to be given to the element. * - The arrary of errors. */ public static function createErrorClass($name, $errors) { if (array_key_exists($name, $errors)) echo 'error'; } /** * This function generates a form INPUT or TEXTAREA tag. * It takes three arguments: * - The name to be given to the element. * - The type of element (text, password). * - An array of errors. */ public static function createFormInput($name, $type, $attributes=array()) { // Assume no value already exists: $value = false; // Check for a value in POST: if (isset($_POST[$name])) $value = $_POST[$name]; // Strip slashes if Magic Quotes is enabled: if ($value && get_magic_quotes_gpc()) $value = stripslashes($value); // Start creating the input: echo '<input type="' . $type . '" name="' . $name . '" id="' . $name . '"'; // Add the value to the input use htmlspecialchars to encode html characters before outputting: if ($value) echo ' value="' . htmlspecialchars($value) . '"'; // Add attributes if(isset($attributes)) { foreach($attributes as $key => $value) { echo ' ' . $key . '="' . $value . '"'; } } echo ' />'; } // End of the create_form_input() function. } // Omit the closing PHP tag to avoid 'headers already sent' errors! I decided to do some modifications to Larry's helper function and also add an attribute array where i can add all the attributes of a html tag. I apologize if this is bad code, if you know better then please enlighten me because its the first time i write anything now. So far my validation all works on the page, so the next step is to take the values and get them added to the database. I will be using the object-orientated method of accessing MySql and i am thinking of adding some kind of classes in here with a Singleton Pattern.
  9. Edward

    My Project Diary

    Regarding the CSS of the project i have used the blueprints.org CSS framework, which Yii Framework also incorporates. Blueprints it is an awesome framework, it has its own ie hacks in it and it also works as a grid where you can have a maximum column width of span-24 which is 950px. You could increase that if you wished to, anyhow if you wish to have a two column, three or possibly five column page this framework makes it easy. It consists of 5 files, one added to override written by the yii developers form.css ie.css main.css (Yii override css and custom form and error styles) print.css screen.css I had a little trouble, spent quite a few hours to be precise trying to work out what yii had done and what they were doing but now i seem to be on some smooth concrete.
  10. Edward

    My Project Diary

    Registration page consisting of: 7 Input's (Name, Address, Password etc) 4 Select Drop down's (State, Country, Birthday Month, Birthday Day) States and countries i put into their own database table to pull out and display in drop down rather than hard coding it. I have implemented a google API into this page called reCaptcha, it works with some basic object orientated code and installing a library, it is used to stop spam computers manually processing the forms and prove we are a human by having us type two words which are unreadable by computer recognition. reCaptcha https://developers.google.com/recaptcha/docs/php Client Side (How to make the CAPTCHA image show up) If you want to use the PHP library to display the reCAPTCHA widget, you'll need to insert this snippet of code inside the <form> element where the reCAPTCHA widget will be placed: require_once('recaptchalib.php'); $publickey = "your_public_key"; // you got this from the signup page echo recaptcha_get_html($publickey); With the code, your form might look something like this: <html> <body> <!-- the body tag is required or the CAPTCHA may not show on some browsers --> <!-- your HTML content --> <form method="post" action="verify.php"> <?php require_once('recaptchalib.php'); $publickey = "your_public_key"; // you got this from the signup page echo recaptcha_get_html($publickey); ?> <input type="submit" /> </form> <!-- more of your HTML content --> </body> </html> Don't forget to set $publickey by replacing your_public_key with your API public key. Note that the value of the "action" attribute is "verify.php". Now, verify.php is the destination file in which the values of this form are submitted to. So you will need a file verify.php in the same location as the client html. The require_once function in the example above expects recaptchalib.php to be in the same directory as your form file. If it is in another directory, you must link it appropriately. For example if your recaptchalib.php is in the directory called "captcha" that is on the same level as your form file, the function will look like this: require_once('captcha/recaptchalib.php'). <?php require_once('recaptchalib.php'); $privatekey = "your_private_key"; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); if (!$resp->is_valid) { // What happens when the CAPTCHA was entered incorrectly die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")"); } else { // Your code here to handle a successful verification } ?> Using reCAPTCHA with PHP The reCAPTCHA PHP Library provides a simple way to place a CAPTCHA on your PHP website, helping you stop bots from abusing it. The library wraps the reCAPTCHA API. To use reCAPTCHA with PHP, you can download reCAPTCHA PHP library. You will only need one file from there (recaptchalib.php). The other files are examples, readme and legal stuff -- they don't affect functionality. Quick Start After you've signed up for your API keys, below are basic instructions for installing reCAPTCHA on your site. A full reference guide to the PHP plugin can be found below. Client Side (How to make the CAPTCHA image show up) If you want to use the PHP library to display the reCAPTCHA widget, you'll need to insert this snippet of code inside the <form> element where the reCAPTCHA widget will be placed: require_once('recaptchalib.php'); $publickey = "your_public_key"; // you got this from the signup page echo recaptcha_get_html($publickey); With the code, your form might look something like this: <html> <body> <!-- the body tag is required or the CAPTCHA may not show on some browsers --> <!-- your HTML content --> <form method="post" action="verify.php"> <?php require_once('recaptchalib.php'); $publickey = "your_public_key"; // you got this from the signup page echo recaptcha_get_html($publickey); ?> <input type="submit" /> </form> <!-- more of your HTML content --> </body> </html> Don't forget to set $publickey by replacing your_public_key with your API public key. Note that the value of the "action" attribute is "verify.php". Now, verify.php is the destination file in which the values of this form are submitted to. So you will need a file verify.php in the same location as the client html. The require_once function in the example above expects recaptchalib.php to be in the same directory as your form file. If it is in another directory, you must link it appropriately. For example if your recaptchalib.php is in the directory called "captcha" that is on the same level as your form file, the function will look like this: require_once('captcha/recaptchalib.php'). Server Side (How to test if the user entered the right answer) The following code should be placed at the top of the verify.php file: <?php require_once('recaptchalib.php'); $privatekey = "your_private_key"; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); if (!$resp->is_valid) { // What happens when the CAPTCHA was entered incorrectly die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." . "(reCAPTCHA said: " . $resp->error . ")"); } else { // Your code here to handle a successful verification } ?> In the code above: recaptcha_check_answer returns an object that represents whether the user successfully completed the challenge. If $resp->is_valid is true then the captcha challenge was correctly completed and you should continue with form processing. If $resp->is_valid is false then the user failed to provide the correct captcha text and you should redisplay the form to allow them another attempt. In this case $resp->error will be an error code that can be provided to recaptcha_get_html. Passing the error code makes the reCAPTCHA control display a message explaining that the user entered the text incorrectly and should try again. Notice that this code is asking for the private key, which should not be confused with the public key. You get that from the same page as the public key. Also make sure your form is set to get the form variables using $_POST, instead of $_REQUEST, and that the form itself is using the POST method. That's it! reCAPTCHA should now be working on your site.</p>
  11. Edward

    My Project Diary

    This week i have worked on my registration process. 1. You fill out your personal details 2. Then you get an email which gives you a unique code to valid your account, so the activation field in the user's table will be set to NULL so you can log in. 3. But once you click the link in your email you then will be directed to a page where you need to pick your username and password. I decided to do it this way because i didn't want people just taking usernames and possibly not clicking the email links and ruining the chance for others that possibly wanting that user name. After the username and password are selected the account will be active and the user can log in.
  12. Edward

    My Project Diary

    Hi my name is Edward, i have decided to document a e-commerce project i will be working on, i have been learning HTML, CSS, PHP, Javascript and MySql for 1 year and 2 months. Books read and experience: Learning PHP, MySQL, and Javascript (O'Reilly) Robin Nixon (Finished) CSS the missing manual (O'Reilly) David Sawyer McFarland(Read parts) Adobe Dreamweaver CS5 Classroom in a Book(Finished) Adobe Fireworks CS5 Classroom in a Book(Finished) Adobe Photoshop CS5 Classroom in a Book(Finished) Beginning PHP and MySQL E-Commerce (Apress) Christian Darie and Emilian Balanescu (Finished 1/3) PHP 5 e-commerce Development (Packt) Michael Peacock (Read a little) Object-Orientated Programming with PHP5 (Packt) Hasin Hayder (Finished 1/3) PHP Object-Orientated Solutions (Friendsof) David Powers (Finished 1/3) Effortless E-Commerce with PHP and MySQL (New Riders) Larry Ullman (Finished 1/2) Agile Web Application Development with Yii1.1 and PHP5 ( Packt) Jeffrey Winesett) (Finished 2/3) Yii 1.1 Application Development Cookbook (Packt) Alexander Makarov (Read parts) PHP and MySQL for Dynamic Web Sites (Peachpit Press) Larry Ullman (Finished) PHP Solutions Dynamic Web Design Made Easy (Friendsof) David Powers (Read Parts) Online: Larry's Forum (Yes here) W3Schools.com Youtube.com I actually have 39 books in total others on javascript, css, jquery, html, mysql etc and more of Larry Ullman's books but these were the ones i gained most of my experience from so far. When i done my first PHP book by Robin Nixon i tried to make a website but i found i was struggling and had to keep looking a lot of stuff up online. I later tried some e-commerce books but i found the first two too complex for me as they were based on Object Orientated frameworks and my OOP code was not such that good and neither i had the experience. I later found a book which i could understand Larry Ullman's Effortless E-commerce so i decided to try that out but someone called Jonathon on this forum recommended that i get more experience before i done this book and give Larry's other book PHP and MySQL for Dynamic Web Sites a read which i did. It took me about 3 months to finish the book with a few breaks in the middle but now i am finally ready to get some practice in and start building. For my first project/coding i will be using a normal way of coding, which means rather than using MVC and having data, logic and views in separate files i will have all my code on the same page. I will be using object-orientated code/classes where i can fit it in because i am working myself towards using the Yii framework or possibly building my own framework which in some ways i think would be better especially for a customized web site like the one i am building. Well that's all i have to say for now, i hope you will enjoy to follow my project. Thank You, Edward
  13. I must of been tired this solution doesn't work, I will try string length function tomorrow.
  14. Okay i got it to work with this // Check for valid province: if (!empty($_POST['province']) && $_POST['province']=='0') { if (preg_match ('/^[A-Z \'.-]{2,50}$/i', $_POST['province'])) { // Save info for database } else { $reg_errors['province'] = 'Please enter a valid province'; } }
  15. I have tried to use strval below before adding it into the empty() function, it still doesn't work, so it seems that empty reads a 0 as either an interger or a string as still a FALSE. I couldn't find a typecast for strings so now i think the only solution is using a conditional here. strval — Get string value of a variable Report a bug Description string strval ( mixed $var ) Get the string value of a variable. See the documentation on string for more information on converting to string. This function performs no formatting on the returned value. If you are looking for a way to format a numeric value as a string, please see sprintf() or number_format().
  16. Right now I understand, i also want to take part in answering the questions but now i am tired after my own project work and need to take a break from looking at code. I agree with you on that the fact that answering all the questions can keep you sharpened up. I guess you will be able to pass all your web knowledge onto your kids. Okay i have done a bit more work, this was my code: // Check for valid province: if (!empty($_POST['province'])) { if (preg_match ('/^[A-Z \'.-]{2,50}$/i', $_POST['province'])) { // Save info for database } else { $reg_errors['province'] = 'Please enter a valid province'; } } Now i found that empty will be true if an integer value of zero is entered, you see this form field isn't required so i am only running the validation on it if a value is entered. Anyhow i am going to the value converted to a string before i run it through the empty then onto pregmatch that way it will work.
  17. Thanks, do you mind me asking, how come you have so much time to answer all these questions on here? (I will do some updates today on my own project, i want to keep a record of how it all goes)
  18. Yes that is correct, I'll be working on this today. So it looks like the solution might be to run a regular expression first then run it through a trim afterwards.
  19. Ah I have read that trim is a string function therefore the value must be quoted to work. Typecasting will not work.
  20. Thanks that has been helpful I'll test it tomorrow and run an echo on the trim see what we come up with.
  21. I used this regular expression in a preg_match, now a value 9 would not pass it neither would 99 or 999, but 0 will go through it. How is that, although I am only accepting A-Z \'.- with a range of 2 - 50? preg_match ('/^[A-Z \'.-]{2,50}$/i', $trimmed['data'])
  22. You would have to implement your own cmodel class so you have access to the validators https://github.com/yiisoft/yii/blob/1.1.11/framework/base/CModel.php
  23. Would it be best to write our own Jquery/Js code or use the validate.js plugin, how would you do it?
  24. Oh right, I did take a look at coffee shop and I could understand what you were doing there with MVC. What I don't like is using someones framework without really understanding how the underlying code is working. I noticed how frameworks load up with a bootstrap always load through index.php then route to the php page you are on. Right now that is all I know but would like to know more before I start using one. Like Antonio said MVC's would be used by larger companies so different skilled employies could work on different parts of the site at the same time. But for what I need to do for now the normal way of logic, view and model on the same page is fine.
×
×
  • Create New...