Validating and Saving a New Member
Submitted by GeePee on Wednesday, April 15, 2015 - 22:57.
In this tutorial, I'm going to show how to validate the user inputs using Javascript and saving the data into MySQL Database using simple PHP. To start in tutorial, first open our last topic called Creating a Registration Page Part I and I made some changes and here’s the modified index file:
And as you can observe, we have added new line of code at the head section. And this is the code:
This code is a link to our javascript file that will be used to validate the user inputs upon submission the registration form.
Then let’s create this javascript file named “registrationformValidation.js” and will save it inside js folder. And add the following code:
Then well create a new PHP file name “register.php” and add the following code:
This code will do the saving data to the MySQL Database tables.
Heres’ the MySQL table structure:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta content="width=device-width, initial-scale=1.0" name="viewport">
- <meta content="" name="description">
- <meta content="" name="author">
- <link href="#" rel="shortcut icon">
- <title>Philsocial</title><!-- Bootstrap core CSS -->
- <link href="css/bootstrap.css" rel="stylesheet"><!-- Custom styles for this template -->
- <link href="jumbotron.css" rel="stylesheet">
- <script type="text/javascript" src="js/registrationformValidation.js"> </script>
- </head>
- <body>
- <div class="navbar navbar-inverse navbar-fixed-top">
- <div class="container">
- <div class="navbar-header">
- <button class="navbar-toggle" data-target=".navbar-collapse" data-toggle="collapse" type=
- "button"><span class="icon-bar"></span> <span class="icon-bar"></span> <span class=
- "icon-bar"></span></button> <a class="navbar-brand" href="#" style=
- "font-weight: bold">Philsocial</a>
- </div>
- <div class="navbar-collapse collapse">
- <form action="home.php" class="navbar-form navbar-right" method="post">
- <div class="form-group">
- <input class="form-control" placeholder="Email" type="text">
- </div>
- <div class="form-group">
- <input class="form-control" placeholder="Password" type="password">
- </div>
- <div class="form-group">
- <button class="btn btn-success" name="login" type="submit">Sign in</button>
- </div>
- </form>
- </div><!--/.navbar-collapse -->
- </div>
- </div><!-- Main jumbotron for a primary marketing message or call to action -->
- <div class="container">
- <div class="rows">
- <div class="col-xs-6">
- <h3>Philsocial helps you connected and share with the other people in your
- life</h3><img src="img/background.png" width="500px"></div>
- <div class="col-xs-6">
- <!--action="register.php" onsubmit="return checkRegistration();"-->
- <form action="register.php" class="form-horizontal" id="register" method="post" onSubmit="return checkRegistration();" >
- <fieldset>
- <legend>Sign Up</legend>
- <h4>It’s free and always will be.</h4>
- <div class="rows">
- <div class="col-xs-12">
- <div class="form-group">
- <div class="rows">
- <div class="col-md-12">
- <div class="col-lg-6" id="divfname">
- <input class="form-control input-lg" id="fName" name="fName" placeholder=
- "First Name" type="text" >
- </div>
- <div class="col-lg-6">
- <input class="form-control input-lg" id="lName" name="lName" placeholder=
- "Last Name" type="text">
- </div>
- </div>
- </div>
- </div>
- <div class="form-group" id="divemail">
- <div class="rows">
- <div class="col-md-12">
- <div class="col-lg-12">
- <input class="form-control input-lg" id="email" name="email"
- placeholder="Your Email" type="text" onblur="checkEmail();">
- </div>
- </div>
- </div>
- </div>
- <div class="form-group" id="divremail">
- <div class="rows">
- <div class="col-md-12">
- <div class="col-lg-12">
- <input class="form-control input-lg" id="reemail" name="reemail"
- placeholder="Re-enter Email" type="text" onblur="checkEmail2();">
- </div>
- </div>
- </div>
- </div>
- <div class="form-group" id="divpass">
- <div class="rows">
- <div class="col-md-12">
- <div class="col-lg-12">
- <input class="form-control input-lg" id="password" name="password"
- placeholder="New Password" type="password">
- </div>
- </div>
- </div>
- </div>
- <div class="form-inline">
- <div class="rows">
- <div class="col-md-12">
- <div class="col-md-3">
- <label>Birthday</label>
- </div>
- <div class="col-lg-3">
- <select class="form-control input-sm" name="month" id="month">
- <option value="">Month</option>
- <?php
- foreach ($m as $month) {
- echo '<option value='.$month.'>'.$month.'</option>';
- }
- ?>
- </select>
- </div>
- <div class="col-lg-3">
- <select class="form-control input-sm" name="day" id="day">
- <option value="">Day</option>
- <?php
- foreach ($d as $day) {
- echo '<option value='.$day.'>'.$day.'</option>';
- }
- ?>
- </select>
- </div>
- <div class="col-lg-3">
- <select class="form-control input-sm" name="yr" id="yr">
- <option value="">Year</option>
- <?php
- foreach ($years as $yr) {
- echo '<option value='.$yr.'>'.$yr.'</option>';
- }
- ?>
- </select>
- </div>
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="rows">
- <div class="col-md-12" style="text-align: left">
- <div class="col-lg-3">
- <div class="radio">
- <label><input checked id="optionsRadios1" name="gender" type=
- "radio" value="Female">Female</label>
- </div>
- </div>
- <div class="col-lg-3">
- <div class="radio">
- <label><input id="optionsRadios2" name="gender" type="radio"
- value="Male"> Male</label>
- </div>
- </div>
- </div>
- </div>
- </div>
- <div class="form-inline">
- <div class="rows">
- <div class="col-md-12">
- <p> By clicking Sign Up, you agree to our Terms and that you have
- read our Data Use Policy, including our Cookie Use.</p>
- </div>
- </div>
- </div>
- <div class="form-group">
- <div class="rows">
- <div class="col-md-8">
- <div class="col-lg-12">
- <button class="btn btn-success btn-lg" type="submit" name="Submit">Sign Up</button>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </fieldset>
- </form>
- </div>
- </div><!--rows-->
- </div><!--container-->
- <hr>
- <footer>
- <p style="text-align: center">© Philsocial 2013</p>
- </footer><!-- /container -->
- <!-- Bootstrap core JavaScript
- ================================================== -->
- <!-- Placed at the end of the document so the pages load faster -->
- <script src="assets/js/jquery.js"></script>
- <script src="js/bootstrap.min.js"></script>
- </body>
- </html>
- <script type="text/javascript" src="js/registrationformValidation.js"> </script>
- function checkRegistration(){
- //this line gets the specific value from every fields
- var f_name = document.forms["register"]["fName"].value;
- var l_name = document.forms["register"]["lName"].value;
- var email = document.forms["register"]["email"].value;
- var reemail = document.forms["register"]["reemail"].value;
- var password = document.forms["register"]["password"].value;
- //check if every fields is equal to null or empty it will return this function to false
- if (f_name == '' || l_name == '' || email == '' || reemail == '' || password == ''){
- alert("You must fill in all of the fields.");
- window.location = "index.php";
- return false;
- }
- //get the selected value for month
- //get the index based on the selected item
- var x = document.getElementById("month").selectedIndex;
- //get the specific value based on the selectedIndex
- var choice = document.getElementsByTagName("option")[x].value;
- //get the selected value for Day
- var y = document.getElementById("day").selectedIndex;
- var choice1 = document.getElementsByTagName("option")[y].value;
- //get the selected value for Year
- var z = document.getElementById("yr").selectedIndex;
- var choice2 = document.getElementsByTagName("option")[z].value;
- //check if the selected value is empty then it will return the function to false
- if (choice == "" || choice1 == "" || choice2 == ""){
- alert("You must indicate your full birthday to register.");
- return false;
- }
- }
- function checkEmail(){
- //get value of email address inputted by the user
- var e_mail = document.getElementById("email").value;
- //get the position of at symbol
- var atpos = e_mail.indexOf("@");
- //get the position of dot
- var dotpos = e_mail.lastIndexOf(".");
- //get the class name of div of email
- var divemail = document.getElementById('divemail');
- if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length){
- // if the javascript detect the bad inputs from user
- //will set the class Name of error
- //then you can notice that the color of email textbox will turn to red
- divemail.className = "form-group has-error";
- //and set the focus to the email
- document.getElementById('email').focus();
- }else{
- //set the class name to success
- //the color will turn to green
- divemail.className = "form-group has-success";
- }
- }
- function checkEmail2(){
- var email = document.forms["register"]["email"].value;
- var divreemail = document.getElementById('divremail');
- var divemail = document.getElementById('divemail');
- var reemail = document.getElementById("reemail").value;
- //check if the re enter the email address is equal to the inputted email
- if(email != reemail){
- divreemail.className = "form-group has-error";
- document.getElementById("reemail").focus();
- }else{
- divreemail.className = "form-group has-success";
- divemail.className = "form-group has-success";
- }
- }
- <?php
- //set up mysql connection
- //select database
- $f_name = $_POST['fName'];
- $l_name = $_POST['lName'];
- $email = $_POST['email'];
- $month = $_POST['month'];
- $day = $_POST['day'];
- $yr = $_POST['yr'];
- $gender = $_POST['gender'];
- $query = "INSERT INTO `user_info` (`fName`, `lName`, `email`, `pword`, `mm`, `dd`, `yy`, `gender`)
- VALUES ('{$f_name}', '{$l_name}', '{$email}', '{$password}', '{$month}', '{$day}', '{$yr}', '{$gender}')";
- echo "<script type=\"text/javascript\">
- alert(\"New member added successfully.\");
- window.location = \"index.php\"
- </script>";
- } else{
- }
- ?>
- CREATE TABLE IF NOT EXISTS `user_info` (
- `user_id` INT(11) NOT NULL AUTO_INCREMENT,
- `fName` VARCHAR(30) NOT NULL,
- `lName` VARCHAR(30) NOT NULL,
- `email` VARCHAR(40) NOT NULL,
- `pword` VARCHAR(60) NOT NULL,
- `mm` VARCHAR(10) NOT NULL,
- `dd` INT(11) NOT NULL,
- `yy` INT(11) NOT NULL,
- `gender` VARCHAR(10) NOT NULL,
- PRIMARY KEY (`user_id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
Add new comment
- 112 views