i had used php and ajax framework to build signup.php. Below is my whole signup.php code:
<?php
session_start();
// If user is logged in, header them away
if(isset($_SESSION["name"]))
{
header("location: message.php? msg=NO to that weenis");
exit();
}
?>
<?php
$tbl1="users";
// Ajax calls this NAME CHECK code to execute
if(isset($_POST["firstnamecheck"]))
{
include_once("php_includes/db_conx.php");
$firstname = preg_replace('#[^a-z0-9]#i', '', $_POST['firstnamecheck']);
$sql="SELECT id from $tbl1 where name='$firstname' limit 1";
$query=mysql_query($sql);
$fncheck=mysql_num_rows($query);
if (strlen($firstname) < 3 || strlen($firstname) > 16)
{
echo '<strong style="color:#FF0000; font-size:20px; ">3 - 16 characters please</strong>';
exit();
}
if (is_numeric($firstname[0]))
{
echo '<strong style="color:#F00; font-size:20px">Name must begin with a letter</strong>';
exit();
}
if ($fncheck < 1 )
{
echo '<strong style="color:#009900; font-size:20px;">' . $firstname . ' is OK</strong>';
exit();
}
else
{
echo '<strong style="color: #F00; font-size:20px">' . $firstname . ' is taken</strong>';
exit();
}
}
?>
<?php
$tbl1="users";
// Ajax calls this Email CHECK code to execute
if(isset($_POST["emailcheck"]))
{
include_once("php_includes/db_conx.php");
$email = mysql_real_escape_string($_POST['emailcheck']);
$email1= mysql_real_escape_string($_POST['validemail']);
$sql="SELECT id from $tbl1 where email='$email' limit 1";
$query=mysql_query($sql);
$e_check=mysql_num_rows($query);
$regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
if (preg_match($regex, $email1) && $e_check < 1 )
{
echo '<strong style="color:#009900; font-size:20px"> valid format</strong>';
echo '<strong style="color:#009900; font-size:20px">' . $email . ' is OK</strong>';
exit();
}
else
{
echo '<strong style="color:#F00; font-size:20px"> invalid format</strong>';
echo '<strong style="color:#F00; font-size:20px">' . $email . ' is taken</strong>';
exit();
}
/* if ($e_check < 1 )
{
echo '<strong style="color:#009900; font-size:20px">' . $email . ' is OK</strong>';
exit();
}
else
{
echo '<strong style="color:#F00; font-size:20px">' . $email . ' is taken</strong>';
exit();
}*/
}
?>
<?php
$tbl1="users";
//$useropt="useroptions";
// Ajax calls this REGISTRATION code to execute
if(isset($_POST["fn"]))
{
// CONNECT TO THE DATABASE
include_once("php_includes/db_conx.php");
// GATHER THE POSTED DATA INTO LOCAL VARIABLES
$fn = preg_replace('#[^a-z0-9]#i', '', $_POST['fn']);
$e = mysql_real_escape_string($_POST['e']);
$p1 = $_POST['p1'];
$g = preg_replace('#[^a-z]#', '', $_POST['g']);
$qf = preg_replace('#[^a-z]#', '', $_POST['qf']);
$c = preg_replace('#[^a-z]#i', '', $_POST['c']);
// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
$fsql="SELECT id FROM $tbl1 WHERE name='$fn' LIMIT 1";
$fquery=mysql_query($fsql);
$fn_check=mysql_num_rows($fquery);
// FORM DATA ERROR HANDLING
if($fn == "" || $e == "" || $p1 == "" || $g == "" || $qf == "" || $c == "")
{
echo '<strong style="color:#F00; font-size:20px">The form submission is missing values123';
exit();
}
else if ($fn_check > 0)
{
echo '<strong style="color:#F00; font-size:20px">The name you entered is alreay taken123';
exit();
}
else if (strlen($fn) < 3 || strlen($fn) > 16)
{
echo '<strong style="color:#F00; font-size:20px">Name must be between 3 and 16 characters123';
exit();
}
else if (is_numeric($fn[0]))
{
echo '<strong style="color:#F00; font-size:20px">Name cannot begin with a number123';
exit();
}
else
{
// END FORM DATA ERROR HANDLING
// Begin Insertion of data into the database
// Hash the password and apply your own mysterious unique salt
$p_hash=md5($p1);
// Add user info into the database table for the main site table
$usql="INSERT INTO $tbl1 (name, email, password, gender, qualification, country, IP, member_since, last_seen)
VALUES('$fn','$e','$p_hash','$g','$qf','$c','$ip',now(),now())";
$insql = mysql_query($usql);
$uid = mysql_insert_id($db_conx);
// Establish their row in the useroptions table
/*$upsql="INSERT INTO $useropt (ID, Name, Background) VALUES ('$uid','$fn','original')";
$upquery =mysqli_query($db_conx, $upsql);*/
// Create directory(folder) to hold each user's files(pics,MP3s, etc.)
if (!file_exists("user/$fn"))
{
mkdir("user/$fn", 0755);
}
//Email the user their activation link
$to = "$e";
$from = "notreply@salmandalalweb.cu.cc";
$subject = 'ErrorFree Account Activation';
$message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>ErrorFree Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://ift.tt/1Ae6cIT"><img src="http://ift.tt/1yXtJr8" width="36" height="30" alt="ErrorFree" style="border:none;float:left;"></a>ErrorFree Account Activation</div><div style="padding:24px; font-size:17px;">Hello '.$fn.',<br /><br />Click the link below to activate your account when ready:<br /><br /><a href="http://ift.tt/1Ae6cIV'.$uid.'&fn='.$fn.'&e='.$e.'&p1='.$p_hash.'">Click here to activate your account now</a <br /><br />Login after successful activation using your:<br />* E-mail Address: <b>'.$e.'</b></div></body></html>';
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
mail($to, $subject, $message, $headers);
print_r(error_get_last());
/*if(!filter_var($e, FILTER_VALIDATE_EMAIL))
{
echo "invalid email";
}
*/
/*
function send_email($info){
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Welcome to ErrorFree');
$message ->setFrom(array('auto_responder@salmandalalweb.cu.cc' => 'ErrorFree'));
$message ->setTo(array($info['e'] => $info['fn']));
$message ->setBody($body_plain_txt);
$message ->addPart($body, 'text/html');
$result = $mailer->send($message);
return $result;
}
*/
echo "signup_success";
exit();
}
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta content="UTF-8">
<title>Sign Up</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style/style.css">
<style type="text/css">
#signupform{
margin-top:34px;
}
#signupform > h2
{
text-align:left;
color: #FF0000;
}
#signupform > div{
margin-right:12px;
}
#signupform > div > input, select
{
width: 190px;
float:right;
background:FFF;
margin-right:1100px;
padding:3px;
}
#signupbtn {
font-size:18px;
padding: 10px;
margin-top: 62px;
margin-left: 70px;
}
</style>
<script src="js/main.js"></script>
<script src="js/ajax.js"></script>
<script>
function restrict(elem)
{
var tf = _(elem);
var rx = new RegExp;
if(elem == "email")
{
rx = /[' "]/gi;
}
tf.value = tf.value.replace(rx, "");
}
function emptyElement(x)
{
_(x).innerHTML = "";
}
function checkfirstname()
{
var fn = _("name").value;
if(fn != "")
{
status .innerHTML = 'checking ...';
var ajax = ajaxObj("POST", "signup.php");
ajax.onreadystatechange = function()
{
if(ajaxReturn(ajax) == true)
{
_("fnamestatus").innerHTML = ajax.responseText;
}
}
ajax.send("firstnamecheck="+fn);
}
}
function checkemail()
{
var em = _("email").value;
var emails = _("email").value;
if(em != "")
{
status .innerHTML = 'checking ...';
var ajax = ajaxObj("POST", "signup.php");
ajax.onreadystatechange = function()
{
if(ajaxReturn(ajax) == true)
{
_("emailstatus").innerHTML = ajax.responseText;
}
}
ajax.send("emailcheck="+em+"&validemail="+emails);
}
}
function signup()
{
var fn = _("name").value;
var e = _("email").value;
var p1 = _("pass1").value;
var p2 = _("pass2").value;
var g = _("gender").value;
var qf = _("qualif").value;
var c = _("country").value;
var status = _("status");
if(fn == "" || e == "" || p1 == "" || p2 == "" || g == "" || qf == "" || c == "")
{
status.innerHTML = "Please fill all the fields";
}
else if(p1 != p2)
{
status.innerHTML = "Your password fields do not match";
}
else
{
_("signupbtn").style.display = "none";
status.innerHTML = 'please wait ...1234';
var ajax = ajaxObj("POST", "signup.php");
ajax.onreadystatechange = function()
{
if(ajaxReturn(ajax) == true)
{
if(ajax.responseText = "signup_success")
{
status.innerHTML = ajax.responseText;
_("signupbtn").style.display = "none";
window.scrollTo(0,0);
_("signupform").innerHTML = " Hi "+fn+", Your account has created sucessfully. Now u are free to solve your Errors";
}
}
}
ajax.send("fn="+fn+"&e="+e+"&p1="+p1+"&g="+g+"&qf="+qf+"&c="+c);
}
}
/*function openLevel()
{
_("levelinfo").style.display="block";
emptyElement("status");
}
/*function addEvents(){
_("elemID").addEventListener
("click", func, false);
}
window.onload = addEvents; */
</script>
</head>
<body>
<?php include_once("template_pageTop.php"); ?>
<div id="pageMiddle">
<h2>Signup by completing the following details</h2>
<form name="signupform" id="signupform" onSubmit="return false;">
<div>Name:
<input id="name" type="text" onBlur="checkfirstname()" maxlength="16">
<span id="fnamestatus"></span></div>
<div>Email Address:
<input id="email" type="text" onBlur="checkemail()" onKeyUp="restrict('email')" maxlength="100">
<span id="emailstatus"></span></div>
<div>Password:
<input id="pass1" type="password" onFocus="emptyElement('status')" maxlength="100"></div>
<div>Confirm Password:
<input id="pass2" type="password" onFocus="emptyElement('status')" maxlength="100"></div>
<div>Gender:
<select id="gender" onFocus="emptyElement('status')">
<option value=""></option>
<option value="m">Male</option>
<option value="f">Female</option>
</select></div>
<div>Qualification:
<input id="qualif" type="text" onFocus="emptyElement('status')" maxlength="16"></div>
<div>Country:
<input id="country" type="text" onFocus="emptyElement('status')" maxlength="16"></div>
<button id="signupbtn" onClick="signup()">Create Account</button>
<span id="status"></span>
</form>
</div>
<?php include_once("template_pageBottom.php"); ?>
</body>
</html>
See the code above the answer it fast. db_conx.php is my connection file which connects to my hosting database.
Aucun commentaire:
Enregistrer un commentaire