0

i want to create login system in php using ajax here is my code

jQuery(document).ready(function ($) {


// login function
 $('form#login').on('submit', function(){ 
    var url = document.getElementById('hidden').innerHTML;
    var login_result = $('.login_result');
    var input_data = $('form#login').serialize() + "&action=login";
    $.ajax({
        type: "POST",
        data: input_data,
        url: url + 'lib/my_func.php',
        success: function(responseText){ // Get the result and asign to each cases
            if(responseText == 0){
                login_result.html('<span class="error">Username or Password Incorrect!</span>');
            }
            else if(responseText == 1){
                window.location = 'userpage.php';
            }
            else{
                alert('Problem with sql query');
            }
        }
    });
    return false;
 });




});

and here is my php function

function login()
{
    if(isset($_POST['action']) && $_POST['action'] == 'login')
    {
        $user   =   $_POST['login'];
        $pas    =   $_POST['password'];

        echo $user;

    }
}

but in this php login function there in no output. and if i remove function echo $user is true, how to echo $user in login function please help me

4
  • 2
    you have to call that function login();
    – Aaroniker
    Apr 27, 2014 at 22:36
  • yes i want to call login() function using ajax
    – Sahill
    Apr 27, 2014 at 22:37
  • Yes, just put login(); under the end of the function so that it is actually is called to start your verification process. Apr 27, 2014 at 22:38
  • i am not understand can you clear this please
    – Sahill
    Apr 27, 2014 at 22:40

3 Answers 3

1

you can use:

$('#submit').click(function()
{
    $.ajax({
        type:'POST',
        url: 'your_url_of_php_file',
        data: "name="+$('#field_id').val()+"&email="+$('#field_id').val()+"",
        success:function(result)
        {
           alert(result)
        }
    })
}

and in php file

<?php
  if(isset($_POST['name']))
  {
      $name=$_POST['name'];
      $email=$_POST['email'];
      echo $email." ".$name;
  }
?>
3
  • sir i want to create this like WordPress, if in feature i use 5 or more different ajax request,then we will create septate file of each ajax request?
    – Sahill
    Apr 27, 2014 at 22:44
  • you want to call your php function from ajax?
    – user3415023
    Apr 27, 2014 at 22:46
  • @user1596202 Why do you wish to use a function? It seems that you are looking to run a functionality. This answer provides that.
    – superhero
    Apr 28, 2014 at 0:16
1

Use your js code as well but modify your my_func.php

<?php
   function login() {
     if(isset($_POST['action']) && $_POST['action'] == 'login')
        {
            $user   =   $_POST['login'];
            $pas    =   $_POST['password'];

            echo $user;

        }
    }
    login();    
?>

UPD after creators comment: You can specify which function to call in your ajax request by adding a special param(e.g. now you are using &action=login). The code of my_func.php is shown below:

<?php
       function login() {
         if(isset($_POST['action']) && $_POST['action'] == 'login')
            {
                $user   =   $_POST['login'];
                $pas    =   $_POST['password'];

                echo $user;

            }
        }
        $action = $_GET['action'];
        if ($action == 'login') {
           login();
        } else {
           echo "No action {$action} found";
        }
    ?>
3
  • but sir i want to use functions because in this file i create forget_password function etc.
    – Sahill
    Apr 27, 2014 at 22:42
  • @user1596202 You should make another request for forgot password. Or you may specify which func to call in your ajax request. Apr 27, 2014 at 22:44
  • @user1596202 you are welcome. I've updated my answer, if solution is found, please marked question as resolved by accepting my answer. Apr 27, 2014 at 22:50
0

then you can use:

var input_data = $('form#login').serialize();
$.ajax({
  type:'POST',
  url:'function.php',
  data:"function="+$('#function_name').val()+"&value="+input_data;
  success:function(result)
  {
     alert(result);
  }
})

in php:

<?php
  if(isset($_POST['function']))
   {
     $_POST['function']($_POST['value']); 
   }

  function abc($data)
  {
     //some code
  }

?>

you just need to send function name and its values then in php your function will call auto...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.