• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create Registration Page in PHP/MySQL

Judge Dr3dd

Mecha Pilot Theorist
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
165
Likes
62
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Related code: How to Create Secure Registration Page in PHP/MySQL

This is a simple tutorial that will teach you on how to create a simple registration form using PHP/MySQL with JavaScript for input validation. This tutorial will not teach you on how to create a good design but rather to give you knowledge on how to create a fully functional registration form.

Creating our Table

First we are going to create our database which stores our data.
To create a database:
1. Open phpmyadmin
2. Click create table and name it as simple_login.
3. Then name the database as "registration".
4. After creating a database name, click the SQL and paste the below code.
  1. CREATE TABLE IF

    NOT EXISTS `member` (
  2. `mem_id` int(

    11

    )

    NOT NULL

    AUTO_INCREMENT,
  3. `username` varchar(

    30

    )

    NOT NULL

    ,
  4. `password` varchar(

    30

    )

    NOT NULL

    ,
  5. `fname` varchar(

    30

    )

    NOT NULL

    ,
  6. `lname` varchar(

    30

    )

    NOT NULL

    ,
  7. `address` varchar(

    100

    )

    NOT NULL

    ,
  8. `contact` varchar(

    30

    )

    NOT NULL

    ,
  9. `picture` varchar(

    100

    )

    NOT NULL

    ,
  10. `gender` varchar(

    10

    )

    NOT NULL

    ,
  11. PRIMARY KEY

    (

    `mem_id`)
  12. )

    ENGINE=

    MyISAM DEFAULT

    CHARSET=

    latin1 AUTO_INCREMENT=

    3

    ;

Creating The Form

Next step is to create a form and save it as index.php. To create a form, open your HTML code editor and paste the code below after the tag.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Registration</title>
  6. </head>
  7. <body>

  8. <form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
  9. <table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
  10. <tr>
  11. <td colspan="2">
  12. <div align="center">
  13. <?php
  14. // $remarks=$_GET['remarks'];
  15. if

    (

    !

    isset

    (

    $_GET

    [

    'remarks'

    ]

    )

    )
  16. {
  17. echo

    'Register Here'

    ;
  18. }
  19. if

    (

    isset

    (

    $_GET

    [

    'remarks'

    ]

    )

    &&

    $_GET

    [

    'remarks'

    ]

    ==

    'success'

    )
  20. {
  21. echo

    'Registration Success'

    ;
  22. }
  23. ?>
  24. </div></td>
  25. </tr>
  26. <tr>
  27. <td width="95"><div align="right">First Name:</div></td>
  28. <td width="171"><input type="text" name="fname" /></td>
  29. </tr>
  30. <tr>
  31. <td><div align="right">Last Name:</div></td>
  32. <td><input type="text" name="lname" /></td>
  33. </tr>
  34. <tr>
  35. <td><div align="right">Gender:</div></td>
  36. <td><input type="text" name="gender" /></td>
  37. </tr>
  38. <tr>
  39. <td><div align="right">Address:</div></td>
  40. <td><input type="text" name="address" /></td>
  41. </tr>
  42. <tr>
  43. <td><div align="right">Contact No.:</div></td>
  44. <td><input type="text" name="contact" /></td>
  45. </tr>
  46. <tr>
  47. <td><div align="right">Username:</div></td>
  48. <td><input type="text" name="username" /></td>
  49. </tr>
  50. <tr>
  51. <td><div align="right">Password:</div></td>
  52. <td><input type="text" name="password" /></td>
  53. </tr>
  54. <tr>
  55. <td><div align="right"></div></td>
  56. <td><input name="submit" type="submit" value="Submit" /></td>
  57. </tr>
  58. </table>
  59. </form>
  60. </body>
  61. </html>

Creating our Connection

Next step is to create a database connection and save it as "connection.php". This file is used to connect our form to database. This file serves as a bridge between our form and our database.

  1. <?php
  2. $mysql_hostname

    =

    "localhost"

    ;
  3. $mysql_user

    =

    "root"

    ;
  4. $mysql_password

    =

    ""

    ;
  5. $mysql_database

    =

    "registration"

    ;
  6. $prefix

    =

    ""

    ;

  7. $bd

    =

    mysqli_connect

    (

    $mysql_hostname

    ,

    $mysql_user

    ,

    $mysql_password

    )

    or die

    (

    "Could not connect database"

    )

    ;
  8. mysqli_select_db

    (

    $bd

    ,

    $mysql_database

    )

    or die

    (

    "Could not select database"

    )

    ;
  9. ?>

Writing Our Save Script

Next step is to create our script that save our input data to database and save it as code_exec.php.

  1. <?php
  2. session_start

    (

    )

    ;

  3. include

    (

    'connection.php'

    )

    ;

  4. $fname

    =

    $_POST

    [

    'fname'

    ]

    ;
  5. $lname

    =

    $_POST

    [

    'lname'

    ]

    ;
  6. $mname

    =

    $_POST

    [

    'mname'

    ]

    ;
  7. $address

    =

    $_POST

    [

    'address'

    ]

    ;
  8. $contact

    =

    $_POST

    [

    'contact'

    ]

    ;
  9. $username

    =

    $_POST

    [

    'username'

    ]

    ;
  10. $password

    =

    $_POST

    [

    'password'

    ]

    ;

  11. mysqli_query

    (

    $bd

    ,

    "INSERT INTO member(fname, lname, gender, address, contact, picture, username, password)VALUES('$fname

    ', '$lname

    ', '$mname

    ', '$address

    ', '$contact

    ', '$pic

    ', '$username

    ', '$password

    ')"

    )

    ;

  12. header

    (

    "location: index.php?remarks=success"

    )

    ;

  13. mysqli_close

    (

    $con

    )

    ;
  14. ?>

Validating The Input

To add some input validation using javascript, add the code below in the head tag of your index.php file. Input validation is used to make sure that all input field are filled out before saving the to database.

  1. <

    script type=

    "text/javascript"

    >
  2. function

    validateForm(

    )
  3. {
  4. var

    a=

    document.forms

    [

    "reg"

    ]

    [

    "fname"

    ]

    .value

    ;
  5. var

    b=

    document.forms

    [

    "reg"

    ]

    [

    "lname"

    ]

    .value

    ;
  6. var

    c=

    document.forms

    [

    "reg"

    ]

    [

    "gender"

    ]

    .value

    ;
  7. var

    d=

    document.forms

    [

    "reg"

    ]

    [

    "address"

    ]

    .value

    ;
  8. var

    e=

    document.forms

    [

    "reg"

    ]

    [

    "contact"

    ]

    .value

    ;
  9. var

    f=

    document.forms

    [

    "reg"

    ]

    [

    "username"

    ]

    .value

    ;
  10. var

    g=

    document.forms

    [

    "reg"

    ]

    [

    "password"

    ]

    .value

    ;
  11. if

    (

    (

    a==

    null

    ||

    a==

    ""

    )

    &&

    (

    b==

    null

    ||

    b==

    ""

    )

    &&

    (

    c==

    null

    ||

    c==

    ""

    )

    &&

    (

    d==

    null

    ||

    d==

    ""

    )

    &&

    (

    e==

    null

    ||

    e==

    ""

    )

    )
  12. {
  13. alert(

    "All Field must be filled out"

    )

    ;
  14. return

    false

    ;
  15. }
  16. if

    (

    a==

    null

    ||

    a==

    ""

    )
  17. {
  18. alert(

    "First name must be filled out"

    )

    ;
  19. return

    false

    ;
  20. }
  21. if

    (

    b==

    null

    ||

    b==

    ""

    )
  22. {
  23. alert(

    "Last name must be filled out"

    )

    ;
  24. return

    false

    ;
  25. }
  26. if

    (

    c==

    null

    ||

    c==

    ""

    )
  27. {
  28. alert(

    "Gender name must be filled out"

    )

    ;
  29. return

    false

    ;
  30. }
  31. if

    (

    d==

    null

    ||

    d==

    ""

    )
  32. {
  33. alert(

    "address must be filled out"

    )

    ;
  34. return

    false

    ;
  35. }
  36. if

    (

    e==

    null

    ||

    e==

    ""

    )
  37. {
  38. alert(

    "contact must be filled out"

    )

    ;
  39. return

    false

    ;
  40. }
  41. if

    (

    f==

    null

    ||

    f==

    ""

    )
  42. {
  43. alert(

    "username must be filled out"

    )

    ;
  44. return

    false

    ;
  45. }
  46. if

    (

    g==

    null

    ||

    g==

    ""

    )
  47. {
  48. alert(

    "password must be filled out"

    )

    ;
  49. return

    false

    ;
  50. }
  51. }
  52. </

    script>

That's it! You've been successfully created your simple registration form with javascript for the input validation.

Registration using php, input validation using javascript.

Update:

I have created another tutorial using a server side validation. And I am also using PDO to secure the registration page. Click here to see my latest update How to Create Registration Page in PHP/MySQL Using PDO Query.

Related code: How to Create Secure Registration Page in PHP/MySQL

Pagination

  • Current page
    1
  • Page
    2
  • Page
    3
  • Next page
    next
  • Last page
    last

 

452,496

338,631

338,639

Top