create php login script using javascript ajax ?
i have this script for php login and i need to use ajax to login the users
but i can not make it work i can't get response form processing page
and i use alert function to alert response text but it's showing to me all my html code in alert
so how can i fix it
my code is seberated into two pages
1- login.php
2- login_processing.php
login.php
<?php
require_once ("../core/initialize.php");
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" http-equiv="Content-Type" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="../style/normalize.css" />
<link rel="stylesheet" type="text/css" href="../style/form.css" />
<link rel="stylesheet" href="../style/Bootstrap-RT/css/bootstrap.rtl.css" type="text/css" />
<link rel="stylesheet" href="../style/Bootstrap-RTL/css/bootstrap.rtl.full.css" type="text/css" />
<title> | </title>
</head>
<body>
<div class="container">
<div class="login-form">
<div>
<?php if(isset($message)): ?>
<div class="text-center text-danger" id="error-message" >
<label><?php echo $message; ?></label>
</div>
<?php endif; ?>
</div>
<form method="post" id="login-form" name="login-form" role="login-form">
<div class="form-group">
<div class="control-label">
<label for="username"> </label>
</div>
<div>
<input type="text" name="user_name" id="user_name" value="" class="form-control" placeholder=" " />
</div>
</div>
<div class="form-group">
<div class="control-label">
<label for="password"> </label>
</div>
<div>
<input type="password" name="password" id="password" value="" class="form-control" placeholder=" " />
</div>
</div>
<div class="form-group">
<div class="form-group">
<button type="submit" name="login-button" id="login-button" class="btn btn-lg btn-default btn-block" >
</button>
</div>
<div class="form-group">
<label>
<a href="send_email"></a>
</label>
</div>
<div class="form-group">
<label>
<a href="signup"></a>
</label>
</div>
</div>
</form>
</div>
</div><!---------container---------->
<script type="text/javascript" src="../js/ajax.js"></script>
<script type="text/javascript" src="../js/functions.js"></script>
<script type="text/javascript" src="../js/jquery-3.1.1.js"></script>
<script type="text/javascript">
function examineeLogin() {
var user_name = document.getElementById("user_name").value;
var password = document.getElementById("password").value;
var error_message = document.getElementById("error-message");
var input_parameters = "user_name=" + user_name + "&password=" + password;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "login_processing.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (xmlhttp.responseText == " ") {
//document.getElementById("error-message").innerHTML = " ";
alert(xmlhttp.responseText);
} else {
//console.log(xmlhttp.responseText);
window.location.href = "../examinee/examinee_profile.php";
//setTimeout(' window.location.href = "../pages/main_page.php"; ',4000);
}
}
};
xmlhttp.send(input_parameters);
}
var form_submti = document.getElementById("login-form");
form_submti.addEventListener("submit", examineeLogin);
</script>
</body>
</html>
login_processing.php
<?php require_once ("../core/initialize.php"); ?>
<?php
$form_errors = array();
$is_valid = true ;
$output = null;
if (isset($_POST["user_name"]) && isset($_POST["password"])) {
$user_name = trim(Functions::POST("user_name")) ;
$password = trim(Functions::POST("password")) ;
$required_fields = array(
" " => $user_name,
" " => $password
);
Validation::check_empty_fields($required_fields);
if(!empty(Validation::form_errors($form_errors))){
$session->message(Validation::form_errors($form_errors));
Functions::redirect_to("login.php");
$is_valid = false ;
}
if($is_valid == true){
if(Examinee::examinee_login($user_name, $password) === true ){
Functions::redirect_to("../examinee/examinee_profile.php");
}else{
$session->message(" ");
Functions::redirect_to("login.php");
}
}
}// End of submit
?>
0 Answer(s)