PHP Tutorials For Beginners Creating a registration form using PHP Creating PHP registration form could be stressful but this tutorial has made it easy for every beginners. When we say “creating a registration form”, we meant creating a membership website or any other type of website with the intention to have the website’s visitor to register or create an account to get your services, like e-Commerce, Social Network and Forum websites etc. If any of the mentioned or related website is your project then this is the right tutorials for you. We are going to walk you through every aspect of creating a membership based site, with a secure member’s area protected by password. Creating membership or related website, there are two major parts involved: user registration and user authentication. In the first part, we are going to cover creation of the registration form and storing the data in a MySQL database. In the second part, we will create the login form and use it to allow users access in the secure area. In order to create a user account, we need to gather a minimal amount of information from the user. We need his name, his email address and his desired username and password. Of course, we can ask for more information at this point, but a long form is always a turn-off. So let’s limit ourselves to just those fields. Here is the registration form: (Insert these codes on your html editor source code) So, we have text fields for name, email and the password. Note that we are using the Password Widget for better usability. Form validation At this point it is a good idea to put some form validation code in place, so we make sure that we have all the data required to create the user account. We need to check if name and email, and password are filled in and that the email is in the proper format. We can use the free JavaScript form validation script to add form validations quickly and easily, with lesser code. Here is a sample JavaScript validation code to be used for the sample form we created earlier: var frmvalidator = new Validator("register"); frmvalidator.EnableOnPageErrorDisplay(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("name","req","Please provide your name"); frmvalidator.addValidation("email","req","Please provide your email address"); frmvalidator.addValidation("email","email","Please provide a valid email address"); frmvalidator.addValidation("username","req","Please provide a username"); frmvalidator.addValidation("password","req","Please provide a password"); To be on the safe side, we will also have the same validations on the server side too. For server side validations, we will use the PHP form validation script Handling the form submission Now we have to handle the form data that is submitted. Here is the sequence (create a new file name it “fg_membersite.php” for the following codes): function RegisterUser() { if(!isset($_POST['submitted'])) { return false; } $formvars = array(); if(!$this->ValidateRegistrationSubmission()) { return false; } $this->CollectRegistrationSubmission($formvars); if(!$this->SaveToDatabase($formvars)) { return false; } if(!$this->SendUserConfirmationEmail($formvars)) { return false; } $this->SendAdminIntimationEmail($formvars); return true; } First, we validate the form submission. Then we collect and ‘sanitize’ the form submission data (always do this before sending email, saving to database etc). The form submission is then saved to the database table. We send an email to the user requesting confirmation. Then we intimate the admin that a user has registered. Saving the data in the database Now that we gathered all the data, we need to store it into the database. Here is how we save the form submission to the database. function SaveToDatabase(&$formvars) { if(!$this->DBLogin()) { $this->HandleError("Database login failed!"); return false; } if(!$this->Ensuretable()) { return false; } if(!$this->IsFieldUnique($formvars,'email')) { $this->HandleError("This email is already registered"); return false; } if(!$this->IsFieldUnique($formvars,'username')) { $this->HandleError("This UserName is already used. Please try another username"); return false; } if(!$this->InsertIntoDB($formvars)) { $this->HandleError("Inserting to Database failed!"); return false; } return true; } To continue this tutorials visit this link http://site-tutorials.com/how_to_create_php_registration_form.html http://site-tutorials.com/ http://ozitechhost.com/
Related Articles -
Creating, registration, form, using, PHP,
|