Alumni Directory Student Register

{source}
<?php

if (isset(XXXXXX['register']))
{
$flag_error="no";

////////////////////////////////////////////////////////////////////////////////////

$national_id=htmlspecialchars(stripslashes(trim(XXXXXX['national_id'])));

if (empty(XXXXXX['national_id'])) {

$national_id_error="National ID field is required";
$flag_error="yes";

}
elseif(!preg_match("/^[0-9]+$/",$national_id))

{
$national_id_error= "National ID must contain only numbers";

$flag_error="yes";
}
else
{

//check if national id exist in directory conj table
// Get a db connection.
$db1 = JFactory::getDbo();

// Create a new query object.

$query1 = $db1->getQuery(true);

// Select

$query1->select($db1->quoteName(array('id', 'first_name', 'family_name','activate')));

$query1->from($db1->quoteName('alumni_directory'));

$query1->where($db1->quoteName('id_number') . ' = '. $db1->quote($national_id));

// Reset the query using our newly populated query object.

$db1->setQuery($query1);



// Load the results

$row = $db1->loadAssoc();

if($db1->getErrorNum())

{

JError::raiseError("Error",$db1->stderr());

}

elseif($row)

{
if($row['activate']=="1"){

//registered before

$national_id_error="National ID is registered";
$flag_error="yes";
}

} //End elseif($row)
else
{
$national_id_error="Sorry you are not CONJ Alumni";
$flag_error="yes";
}

// Get a db connection.
$db11 = JFactory::getDbo();
// Create a new query object.
$query11 = $db11->getQuery(true);

// Select all records
$query11->select($db11->quoteName(array('id', 'username')));
$query11->from($db11->quoteName('#__users'));
$query11->where($db11->quoteName('username').' LIKE '.$db11->quote($national_id));

// Reset the query using our newly populated query object.
$db11->setQuery($query11);

// Load the results
$results11 = $db11->loadRow();
if($db11->getErrorNum())

{

JError::raiseError("Error",$db11->stderr());

}
elseif($results11)

{
$national_id_error="National ID is registered";
$flag_error="yes";

}

} //End else


////////////////////////////////////////////////////////////////////////////////////

if (empty($_POST['email'])) {
$email_error = "Email field is required";

$flag_error="yes";
} else {
$email = htmlspecialchars(stripslashes(trim($_POST['email'])));
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$email_error = "Invalid email format";

$flag_error="yes";
}
}
////////////////////////////////////////////////////////////////////////////////////
if (empty($_POST["password1"])||empty($_POST["password2"])) {

$password_error="Password and Confirm Password fields are required";

$flag_error="yes";
}

else

{
$password1=htmlspecialchars(stripslashes(trim($_POST['password1'])));
$password2=htmlspecialchars(stripslashes(trim($_POST['password2'])));

if($password1!=$password2)

{

$password_error="Password and Confirm Password should match";

$flag_error="yes";

}

}

////////////////////////////////////////////////////////////////////////////////////
if($flag_error=="no")

{

//insert in table users

$register_date=date("Y-m-d h:i:sa");

$update_date=date("Y-m-d h:i:sa");

$last_visit_date=date("Y-m-d h:i:sa");
$usertype="";

$block=0;

$sendEmail=0;
$activation="";

$params="{}";

$lastResetTime=date();

$resetCount=0;
$name=$row['first_name'].' '.$row['family_name'];
jimport('joomla.user.helper');


$salt = JUserHelper::genRandomPassword(32);
$crypt = JUserHelper::getCryptedPassword($password1, $salt);
$password = $crypt.':'.$salt;
////////////////////////////////////////////////////////////////////////////////////////////////////////

// Get a db connection.
$db12 = JFactory::getDbo();

// Create a new query object.
$query12 = $db12->getQuery(true);

// Insert columns.
$columns = array('name', 'username', 'password', 'email','usertype','block','sendEmail','registerDate','lastvisitDate','activation','params','lastResetTime','resetCount');

// Insert values.
$values = array($db12->quote($name), $db12->quote($national_id), $db12->quote($password), $db12->quote($email), $db12->quote($usertype), $db12->quote($block), $db12->quote($sendEmail), $db12->quote($registerDate), $db12->quote($lastvisitDate), $db12->quote($activation), $db12->quote($params), $db12->quote($lastResetTime), $db12->quote($resetCount));

// Prepare the insert query.
$query12
->insert($db12->quoteName('#__users'))

->columns($db12->quoteName($columns))
->values(implode(',', $values));

// Set the query using our newly populated query object and execute it.
$db12->setQuery($query12);
$db12->execute();
if($db12->getErrorNum())
{
JError::raiseError("Error",$db12->stderr());
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
else
{

//get id from table users

// Get a db connection.
$db6 = JFactory::getDbo();

// Create a new query object.
$query6 = $db6->getQuery(true);

// Select all records
//$query6->select($db6->quoteName(array('id', 'username','name','registerDate','email')));

$query6->select($db6->quoteName(array('id', 'username','name','registerDate')));
$query6->from($db6->quoteName('#__users'));
$query6->where($db6->quoteName('username').' LIKE '.$db6->quote($national_id));

// Reset the query using our newly populated query object.
$db6->setQuery($query6);

// Load the results
$results = $db6->loadAssoc();
//print_r($results);

if($results)
{

$id1=$results['id'];

$name=$results['name'];

//$registered_date_all=$results['registerDate'];

//$registered_date_all=strtotime( $registered_date_all);

//$registered_date= date("Y-m-d",$registered_date_all);

//$update_date=$registered_date;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

//insert in table usergroup

// Get a db connection.
$db5 = JFactory::getDbo();

// Create a new query object.
$query5 = $db5->getQuery(true);

// Insert columns.
$columns = array('user_id', 'group_id');

// Insert values.
$values = array($db5->quote($id1), $db5->quote('12'));


// Prepare the insert query.
$query5
->insert($db5->quoteName('#__user_usergroup_map'))

->columns($db5->quoteName($columns))
->values(implode(',', $values));

// Set the query using our newly populated query object and execute it.
$db5->setQuery($query5);
$db5->execute();
if($db5->getErrorNum())
{
JError::raiseError("Error",$db5->stderr());
}
else
{
////////////////////////////////////////////////////////////////////////////////////////////////////////
//update alumni directory table
$user = JFactory::getUser();

if($user->guest) {
$user_id="0";
}
else{$user_id=$user->id;}

$db = JFactory::getDbo();

$query = $db->getQuery(true);

// Fields to update.
$fields = array(
$db->quoteName('password') . ' = ' . $db->quote($password),
$db->quoteName('student_register_date') . ' = '.$db->quote($register_date),
$db->quoteName('update_date') . ' = '.$db->quote($update_date),
$db->quoteName('by_whom_student_register') . ' = '.$db->quote($user_id),
$db->quoteName('last_visit_date') . ' = '.$db->quote($last_visit_date),
$db->quoteName('activate') . ' = 1',
$db->quoteName('users_id') . ' = '.$db->quote($id1)
);

// Conditions for which records should be updated.
$conditions = array(
$db->quoteName('id') . ' = ' . $db->quote($row['id'])
);

$query->update($db->quoteName('alumni_directory'))->set($fields)->where($conditions);

$db->setQuery($query);

$db->execute();
if($db->getErrorNum())
{
JError::raiseError("Error",$db->stderr());
}
else
{ ?><div class="alert alert-success">The registration is submitted successfully. Go to 
<a href="/./alumni-directory-student-login" >login page</a></div><?php


// define variables and set to empty values
$email_error=$password_error=$national_id_error= "";

$email=$national_id=$password1=$password2= "";

}
}//End else
} //End if($results)
} //End else

} //End if($flag_error=="no")

///////////////////////////////////////////////////////////////////////////////////////////////////
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="registration">

<form id="register_form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" class="form-validate form-horizontal" onsubmit="return checkForm(this);">
<fieldset>
<div class="legend"><legend>Alumni Registration</legend></div>
<div class="form-group">

<div class="col-sm-4 col-md-4 col-lg-4 col-xs-12 control-label">
<span class="spacer"><span class="before"></span><span class="text"><label id="jform_spacer-lbl" class=""><strong class="red">*</strong> Required field</label></span><span class="after"></span></span> </div>
<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12">
</div>

</div>
<div class="form-group">

<div class="col-sm-4 col-md-4 col-lg-4 col-xs-12 control-label">
<label id="national_id-lbl" for="national_id" class="hasTip required" title="">National ID:<span class="star">&nbsp;*</span></label> </div>
<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12">
<input type="text" name="national_id" id="national_id" value="<?php echo $national_id; ?>" class="required" size="30" aria-required="true" onkeypress='return event.charCode >= 48 && event.charCode <= 57' required /> </div>

</div>
<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12 col-lg-offset-4 col-md-offset-4 col-sm-offset-4"><div id="national_id_error_j"></div></div>
<?php if($national_id_error){ ?>
<div class="form-group">


<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12 col-lg-offset-4 col-md-offset-4 col-sm-offset-4">
<div class="alert alert-danger sd10-alert"><?php echo $national_id_error; ?></div>
</div>
</div>
<?php } ?>
<div class="form-group">

<div class="col-sm-4 control-label">
<label id="email-lbl" for="email" class="hasTip required" title="">Email Address:<span class="star">&nbsp;*</span></label> </div>
<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12">
<input type="email" name="email" class="validate-email required" id="email" size="30" aria-required="true" value="<?php echo $email; ?>" required /> </div>

</div>
<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12 col-lg-offset-4 col-md-offset-4 col-sm-offset-4"><div id="email_error_j"></div></div>
<?php if($email_error){ ?>
<div class="form-group">


<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12 col-lg-offset-4 col-md-offset-4 col-sm-offset-4">
<div class="alert alert-danger sd10-alert"><?php echo $email_error; ?></div>
</div>
</div>
<?php } ?>
<div class="form-group">

<div class="col-sm-4 col-md-4 col-lg-4 col-xs-12 control-label">
<label id="password1-lbl" for="password1" class="hasTip required" title="">Password:<span class="star">&nbsp;*</span></label> </div>
<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12">
<input type="password" name="password1" id="password1" autocomplete="off" class="validate-password required" size="30" aria-required="true" value="<?php echo $password1; ?>" required /> </div>

</div>
<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12 col-lg-offset-4 col-md-offset-4 col-sm-offset-4"><div id="password1_error_j"></div></div>
<div class="form-group">

<div class="col-sm-4 col-md-4 col-lg-4 col-xs-12 control-label">
<label id="password2-lbl" for="password2" class="hasTip required" title="">Confirm Password:<span class="star">&nbsp;*</span></label> </div>
<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12">
<input type="password" name="password2" id="password2" autocomplete="off" class="validate-password required" size="30" aria-required="true" value="<?php echo $password2; ?>" required /> </div>

</div>
<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12 col-lg-offset-4 col-md-offset-4 col-sm-offset-4"><div id="password2_error_j"></div></div>
<?php if($password_error){ ?>
<div class="form-group">


<div class="col-sm-8 col-md-8 col-lg-8 col-xs-12 col-lg-offset-4 col-md-offset-4 col-sm-offset-4">
<div class="alert alert-danger sd10-alert"><?php echo $password_error; ?></div>
</div>
</div>
<?php } ?>

</fieldset>

<hr />

<strong>Note:</strong> For any problems please contact us on: Phone: +966 12 22466666 - ext : 46120 or by Email: <a href="mailto:ashisu@ngha.med.sa">ashisu@ngha.med.sa</a>

<div class="form-group form-actions">
<input type="submit" class="btn btn-primary validate" value="Register" name="register" />
<a href="/./alumni-directory-student-login" class="btn btn-default cancel" style="text-decoration:none;">Cancel</a>
</div>
</form>
</div>

<script type="text/javascript">


document.getElementById("national_id").addEventListener("blur", validate_national_id);
document.getElementById("email").addEventListener("blur", validate_email);

document.getElementById("password1").addEventListener("blur", validate_password1);

document.getElementById("password2").addEventListener("blur", validate_password2);
///////////////////////////////////////////////////////////////////////////////////////////////////////
function validate_national_id() {

var d = document.getElementById("national_id_error_j");

$("#national_id_error_j").text("");
var national_id= $("#national_id").val();

if(!checkEmpty(register_form.national_id))

{

$("#national_id_error_j").text("National ID field is required");

d.className="alert alert-danger sd10-alert java-error";

$("#national_id_error_j").css("margin-bottom", "20px");

return false;

}
if(!/^[0-9]+$/.test(national_id))
{
$("#national_id_error_j").text("National ID must contain only numbers");

d.className="alert alert-danger sd10-alert";

$("#national_id_error_j").css("margin-bottom", "20px");

return false;
}

d.className="";
$("#national_id_error_j").css("margin-bottom", "0px");
return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////

function validate_password1() {

var d = document.getElementById("password1_error_j");

var d1 = document.getElementById("password2_error_j");

$("#password1_error_j").text("");
var password1 = $("#password1").val();

var password2 = $("#password2").val();

if(!checkEmpty(register_form.password1))

{

$("#password1_error_j").text("Password field is required");

d.className="alert alert-danger sd10-alert java-error";

$("#password1_error_j").css("margin-bottom", "20px");

return false;

}

else if(password1 != password2)

{

$("#password2_error_j").text("Password and Confirm Password should match");

d1.className="alert alert-danger sd10-alert java-error";

$("#password2_error_j").css("margin-bottom", "20px");

d.className="";

$("#password1_error_j").css("margin-bottom", "0px");

return false;

}

$("#password2_error_j").text("");


d.className="";
$("#password1_error_j").css("margin-bottom", "0px");

d1.className="";
$("#password2_error_j").css("margin-bottom", "0px");
return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////

function validate_password2() {

var d = document.getElementById("password2_error_j");

$("#password2_error_j").text("");

var password2 = $("#password2").val();

var password1 = $("#password1").val();

if(!checkEmpty(register_form.password2))

{

$("#password2_error_j").text("Confirm Password field is required");

d.className="alert alert-danger sd10-alert java-error";

$("#password2_error_j").css("margin-bottom", "20px");

return false;

}

else if(password1 != password2)

{

$("#password2_error_j").text("Password and Confirm Password should match");

d.className="alert alert-danger sd10-alert java-error";

$("#password2_error_j").css("margin-bottom", "20px");
return false;

}

d.className="";
$("#password2_error_j").css("margin-bottom", "0px");

return true;

}

///////////////////////////////////////////////////////////////////////////////////////////////////////
function validate_email() {

var d = document.getElementById("email_error_j");

$("#email_error_j").text("");
var email = $("#email").val();

if(!checkEmpty(register_form.email))

{

$("#email_error_j").text("Email field is required");

d.className="alert alert-danger sd10-alert";

$("#email_error_j").css("margin-bottom", "20px");

return false;

}


if (!validateEmail()) {
$("#email_error_j").text(email + " is not valid email");

d.className="alert alert-danger sd10-alert";

$("#email_error_j").css("margin-bottom", "20px");

return false;
}

d.className="";
$("#email_error_j").css("margin-bottom", "0px");

return true;
}


function validateEmail() {

var x = document.getElementById("email").value;

var atpos = x.indexOf("@");
var dotpos = x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) {
//alert("Not a valid e-mail address");
return false;
}
else{return true;}
}



////////////////////////////////////////////////////////////////////////////////////////////////////////

function checkEmpty(field)
{

var allowBlank = false;
if(!allowBlank && field.value == '') {
//field.focus();
return false;
}
return true;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkForm(form)
{

if(!validate_national_id()){return false;}

if(!validate_email()){return false;}

if(!validate_password1()){return false;}

if(!validate_password2()){return false;}
return true;
}
</script>
{/source}