Free Ajax/jquery Register script

C++, C#, Java, PHP, ect...
Post Reply
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Free Ajax/jquery Register script

Post by hallsofvallhalla »

Was working on my register script using ajax and jquery and thought I would pass it on. This is the barebones so feel free to add on

Some of you probably already use this method but basically it allows you too register without refresh using POST. It serializes teh form and sends it to the php file which then does the normal register then whatever you echo is returned back to the function and you can do what you want with it with the response variable. You can turn this to JSON data by changing the datatype.

javascirpt function to go on your html page

Code: Select all

function Register()
{
if($("#registerForm input[name=password]").val() != $("#registerForm input[name=password2]").val())
{
	$("#regMessage").show();
	document.getElementById('regMessage').innerHTML = "Passwords do not match";
        $("#regMessage").fadeOut(2000);
}
else
{
dataString = $("#registerForm").serialize();
 var query = $.ajax({
               url:'ajax/register.php',
               dataType: "html",
	       type: "post",
	       data: dataString
           });
           query.done(function(response,textStatus,jqXHR)
           {
		$("#regMessage").show();
                document.getElementById('regMessage').innerHTML = response;
	        $("#regMessage").fadeOut(2000);
           });
}
          
}
form html

Code: Select all

 <form id='registerForm' class="register_form" action="Javascript:Register()" method="post">
       <label>Desired Nickname:</label><input type="text" name="username">
            <label>Desired Password:</label><input type="password" name="password">
	    <label>Re-Type Password:</label><input type="password" name="password2">
	    <div id='regMessage'></div>
            <input type="submit" name="LogIn" value="Register">
             </form>
register.php

Code: Select all

<?php
include 'connect.php';
?>
  <link href="style.css" rel="stylesheet" type="text/css" />
<div id="logintable">
  <?php
   $user=$_POST['username'];
   $password=$_POST['password']; 
  $isuser="SELECT * from users where name='$user'";
  $isuser2=mysql_query($isuser) or die("Could not query users table");
  $isuser3=mysql_fetch_array($isuser2);
  if(!$_POST['password'])
  {
     print "<center>You did not enter a password<br>";
     exit;
  }
  else if($isuser3 || strlen($user)>15 || strlen($user)<1)
  {
     print "<center>There is already a user of that name or the name you specified is over 16 letters or less than 1 letter<br>";
     exit;
  }
      else
    {  
      $password=md5($password);     	  
      $SQL = "INSERT into users(name, password) VALUES ('$user','$password')"; 
      mysql_query($SQL) or die("could not register");
      echo "Thank you for Registering. You may now log in.";
}
?>
Post Reply

Return to “Coding”