Get with the ajax data into a php file
- by Max Torstensson
I'm trying to build a login system with ajax and php. I use a log-view where I then save the data in ajax which brings into my doLogin.php (php file). My problem is that php file should never be any ajax data for when I build it into a class and a function
VIEW:
public function DoLoginBox() {
//inloggning form-tagg...
return '<p> </p>
<div id="content">
<h1>Login Form</h1>
<form id="form1" name="form1" action="Handler/doLogin.php" method="post">
<p>
<label for="username">Username: </label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="password">Password: </label>
<input type="password" name="password" id="password" />
</p>
<p>
<input type="submit" id="login" name="login" />
</p>
</form>
<div id="message"></div>
</div>';
}
AJAX:
<script type="text/javascript">
$(document).ready(function() {
$("#login").click(function() {
var action = $("#form1").attr('action');
var form_data = {
username: $("#username").val(),
password: $("#password").val(),
is_ajax: 1
};
$.ajax({
type: "POST",
url: action,
data: form_data,
success: function(response)
{
if(response == 'success')
$("#form1").slideUp('slow', function() {
$("#message").html("<p class='success'>You have logged in successfully!</p>");
});
else
$("#message").html("<p class='error'>Invalid username and/or password.</p>");
}
});
return false;
});
});
</script
PHP:
<?php
require_once ("UserHandler.php");
class DoLogingHandler{
public function Login (){
$is_ajax = !empty($_REQUEST['is_ajax']);
if(isset($is_ajax) && $is_ajax)
{
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
$UserHandler = new UserHandler();
$UserHandler -> controllDB($username,$password);
if($username == 'demo' && $password == 'demo')
{
echo "success";
}
}
}
}
` $DoLogingHandler = new DoLogingHandler();
$DoLogingHandler-Login();
?