i need to send data into my database using ajax but i don't need to send duplicate data so how can i return false if the username or email already exist in my database or prevent submit also i need to prevent submit if the password fields are not matching
and direct after submit using ajax
sorry if you see any sloppy coding i'm still new to programming !!!
<script type="text/javascript" src="functions.js"></script>
<script type="text/javascript" src="jquery-1.12.3.min.js"></script>
<?php require("connection.php"); ?>
<?php
/*///////////////////////////////////
********
********
******** check username if it's exist in your Database
********
********
*////////////////////////////////////
if(isset($_POST["check_name"]) && $_POST["check_name"] != ""){
$username = preg_replace('#[^a-z0-9]#i', '', $_POST['check_name']);
$sql_uname_check = mysqli_query($link,"SELECT `examinee_id` FROM `examinee` WHERE `user_name`='$username' LIMIT 1");
$uname_check = mysqli_num_rows($sql_uname_check);
if (strlen($username) < 4) {
echo '4 - 15 characters please';
exit();
}
if (is_numeric($username[0])) {
echo 'First character must be a letter';
exit();
}
if ($uname_check < 1) {
echo '<strong>' . $username . '</strong> is OK';
exit();
} else {
echo '<strong>' . $username . '</strong> is taken';
exit();
}
}
/*///////////////////////////////////
********
********
******** check E-mail if it's exist in your Database
********
********
*////////////////////////////////////
if(isset($_POST["email_check"]) && $_POST["email_check"] != ""){
$email = ($_POST['email_check']);/*preg_replace('#[^a-z0-9]#i', '',*/
$sql_uname_check = mysqli_query($link,"SELECT `examinee_id` FROM `examinee` WHERE `E-mail`='".$email."' LIMIT 1");
$uname_check = mysqli_num_rows($sql_uname_check);
if(!preg_match("^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$^",$email)){
echo 'invalid email format';
exit();
}
if ($uname_check < 1) {
echo '<strong>' . $email . '</strong> is OK';
exit();
} else {
echo '<strong>' . $email . '</strong> is taken';
exit();
}
}
/*///////////////////////////////////
********
********
******** Define variables
********
********
*////////////////////////////////////
if( isset($user_name)&& isset($email) && isset( $password ) ){
$user_name =mysqli_real_escape_string($link,stripslashes($_POST['user_name'])) ;
$email = mysqli_real_escape_string($link,stripslashes($_POST['email']));
$password = $_POST['password'];} else{$password= "";
$confirmpassword = $_POST['confirmpassword'];
date_default_timezone_set('US/Eastern');
$currtime = time();
$date = date('Y-m-d', $currtime);
$salty = dechex($currtime).$password;
$salted = hash('sha1', $salty);
$query = mysqli_query($link,"INSERT INTO `examinee`(`examinee_id`, `user_name`, `E-mail`, `password`, `date_of_registration`)
VALUES
(NULL,'".$user_name."','".$email."','".$salted."','".$date."' ) ") or die (mysqli_error($link));
if($query){
header("location:http://localhost/online_test/index.php");
echo " <br>";
}
else{
echo "Error! <br>";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="RTL" lang="Ar">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link href="xxx.css" media="screen" rel="stylesheet" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<style>
#passwordStrength
{
height:10px;
display:block;
float:right;
}
.strength0
{
*width:250px;
*background:#cccccc;
}
.strength1
{
width:50px;
background:#ff0000;
}
.strength2
{
width:100px;
background:#ff5f5f;
}
.strength3
{
width:150px;
background:#56e500;
}
.strength4
{
background:#4dcd00;
width:200px;
}
.strength5
{
background:#399800;
width:250px;
}
.error{
padding-left: 40px;
border:1px solid ;
}
</style>
</head>
<?php require("processes.php"); ?>
<body>
<div class="continar">
<div><span id="error_message"></span></div>
<div class="login">
<form method="post" action="" id="myform" >
<p>
<label> </label>
<input type="text" name="user_name" id="user_name" onBlur="checkusername()" maxlength="15">
<span id="usernamestatus" class="error"></span>
</p>
<p>
<label> </label>
<input type="text" name="email" id="email" onBlur="check_Email()" maxlength="50">
<span id="Emailstatus" class="error"></span>
</p>
<p>
<label for="pass"> </label>
<input type="password" name="password" id="mypassword" onkeyup="passwordStrength(this.value)"/>
<label for="passwordStrength" class="error"></label>
<div id="passwordDescription" class="error"></div>
<div id="passwordStrength" class="strength0" class="error"></div>
</p>
<p>
<label> </label>
<input type="password" name="passwordconfirm" id="passwordconfirm" onkeyup="mypasswordconfirm(); return false;">
<span id="confirmMessage" class="error"></span>
</p>
<p>
<input type="submit" name="submit" id="mysubmit"/>
<span id="mysubmit_error" class="error"></span>
</p>
</form>
</div>
</div>
<script src="script.js"></script>
</body>
<html>
// Check username if it's exist
function checkusername(){
var status = document.getElementById("usernamestatus");
var username = document.getElementById("user_name").value;
if(username != ""){
status.innerHTML = 'checking...';
var hr = new XMLHttpRequest();
hr.open("POST", "processes.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
status.innerHTML = hr.responseText;
}
}
var v = "check_name="+username ;
hr.send(v);
}
}
// Check user email if it's exist
function check_Email(){
var status = document.getElementById("Emailstatus");
var Email = document.getElementById("email").value;
if(Email != ""){
status.innerHTML = 'checking...';
var hr = new XMLHttpRequest();
hr.open("POST", "processes.php", true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
status.innerHTML = hr.responseText;
}
}
var v = "email_check="+Email;
hr.send(v);
}
}
// check password strength
function passwordStrength(mypassword){
// var mypassword = document.getElementById("password");
var desc = new Array();
desc[0] = "Very Weak";
desc[1] = "Weak";
desc[2] = "Better";
desc[3] = "Medium";
desc[4] = "Strong";
desc[5] = "Strongest";
var score = 0;
//if password bigger than 6 give 1 point
if (mypassword.length > 6) score++;
//if password has both lower and uppercase characters give 1 point
if ( ( mypassword.match(/[a-z]/) ) && ( mypassword.match(/[A-Z]/) ) ) score++;
//if password has at least one number give 1 point
if (mypassword.match(/\d+/)) score++;
//if password has at least one special caracther give 1 point
if ( mypassword.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) score++;
//if password bigger than 12 give another 1 point
if (mypassword.length > 12) score++;
document.getElementById("passwordDescription").innerHTML = desc[score];
document.getElementById("passwordStrength").className = "strength" + score;
}
// check password matching
function mypasswordconfirm(){
var mypassword = document.getElementById("mypassword");
var mypasswordconfirm = document.getElementById("passwordconfirm");
var message = document.getElementById("confirmMessage");
if(mypassword .value == mypasswordconfirm.value){
message.innerHTML = "password matching";
}
else{
message.innerHTML = "password do not match";
}
}
// check empty fields
function prepareEventHandlers(){
document.getElementById("myform").onsubmit = function(){
var myusername = document.getElementById("user_name");
var myemail = document.getElementById("email");
var myemailconfirm = document.getElementById("emailconfirm");
var mypassword = document.getElementById("password");
var mypasswordconfirm = document.getElementById("passwordconfirm");
var myerrormessage = document.getElementById("error_message");
if(myusername.value == "" || myemail.value == "" || myemailconfirm .value == "" || mypassword.value == "" || mypasswordconfirm.value == "" ){
myerrormessage.innerHTML = " ";
return false ;
}
else{
document.getElementById("mysubmit").style.display = "none";
document.getElementById("mysubmit_error").innerHTML = "please wait..........";
var request = new XMLHttpRequest ();
var url = "processes.php";
var variables = myusername+myemail+mypassword ;
request.open("POST",url,true) ;
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.onreadystatechange = function(){
if(request.readyState == 4 && request.responseText == 200){
var return_data = request.responseText ;
document.getElementById("").innerHTML = return_data ;
}
}
request.send(variables);
}
};
}
window.onload = function(){
prepareEventHandlers();
}
1 Answer(s)