After a long time, I am back with my new Tutorial. In this tutorial we are going to show you PHP login with Ajax and will get response. For making Ajax login first of all you have to make one login form in simple HTML code. The form will consists one two input fields and one input button like below mentioned HTML form. you can also use bootstrap for making this html form for login.
 
Sign In

Writing Ajax Code for Login: -

After completing the HTML form you have to write some coding of Ajax for getting the field value from the field the then transfer to another PHP page. Just take care for that you will have to add JQuery link into your index.php page that consists the JQuery or JavaScript function.
Here is the JQuery function below for getting username and password value.
$(function () {
                $("#loginbutton").click(function () {
                    var usrname = $("#userid").val();
                    var password = $("#password").val();

                    if (usrname == "") {
                        $("#errorblock").html("Please fill user name");
                        $("#errorblock").show();
                        return false;
                    }

                    else if (password == "") {
                        $("#errorblock").html("Please fill your password");
                        $("#errorblock").show();
                        return false;
                    }
                    else {
                        loginuser(usrname, password);
                    }
                });

            });

Ajax Sending URL: -

This is JQuery Validation for field either fields have values or not. Now we are writing Ajax code for sending data to another page for storing.
function loginuser(usrname, password) {//searching MAJOR city by state as in ajax
                $.ajax({type: "GET",
                    url: "confirm_usr.php?usname=" + usrname + "&pass=" + password,
                    async: false,
                    success: function (response) {
                    
                        var response_1 = $.trim(response).charAt();
                        if (response_1 == "f") {
                            $("#errorblock").html("Invalid user name or password");
                            $("#errorblock").show();
                            return false;
                        } else if (response_1 == "s") {
                            window.location = "dashboard.php";
                        }
                    }
                });
            }
			

PHP Login Script: -

Now we are going to write PHP code for storing values into the PHPMYSQL database. While creating the PHP code make sure that you have included database file into the PHP file.
prepare("SELECT id FROM admin_login WHERE  userid =:param1 AND password =:param2");
		$login_query->bindParam("param1",$_GET["usname"]);
		$login_query->bindParam("param2",$_GET["pass"]);
		
		if($login_query->execute()){
			$num_of_rows = $login_query->rowCount();
			if($num_of_rows==1){
				$user_details=$login_query->fetch(PDO::FETCH_ASSOC);
				$userid=$user_details["id"];
				setsession($userid);
				$output = "s";
			}else{
                            $output= "f";
                        }
			
		}
		echo $output;
	}
?>
In this PHP code username and password are matching each other into the database. If they are matching and giving row count 1 then it is sending the user ID into the session and sending the response as output.

This PHP response will return back to the Ajax code where we can define our success code or failure.
If you still Facing any problem the feel free to contact us. just write into the contact form. Don't forget to send feedback.

0 comments :

Post a Comment

 
# Top