Get with the ajax data into a php file

Posted by Max Torstensson on Stack Overflow See other posts from Stack Overflow or by Max Torstensson
Published on 2012-06-30T06:15:41Z Indexed on 2012/06/30 9:16 UTC
Read the original article Hit count: 183

Filed under:
|

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>&nbsp;</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(); ?>

© Stack Overflow or respective owner

Related posts about php

Related posts about AJAX