Benyaman
Benyaman

Reputation: 449

AJAX not returning result from php

I am trying to learn from an example from online,for a login form with php and jquery and i am using the exactly the same example, but for some reason the AJAX isnt getting anything back but redirecting my to another php. Here is a link of what i had been trying and the problem.

http://rentaid.info/Bootstraptest/testlogin.html

It supposed to get the result and display it back on the same page, but it is redirecting me to another blank php with the result on it. Thanks for your time, i provided all the codes that i have, i hope the question isnt too stupid.

HTML code:

<!DOCTYPE HTML>
<html>
    <head>
    </head>
    <body>
        <form id= "loginform" class="form-horizontal" action='http://rentaid.info/Bootstraptest/agentlogin.php' method='POST'>
            <p id="result"></p>
            <!-- Sign In Form -->
            <input required="" id="userid" name="username" type="text" class="form-control" placeholder="Registered Email" class="input-medium" required="">
            <input required="" id="passwordinput" name="password" class="form-control" type="password" placeholder="Password" class="input-medium">
            <!-- Button -->
            <button id="signinbutton" name="signin" class="btn btn-success"  style="width:100px;">Sign In</button>
        </form>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <script type="text/javasript" src="http://rentaid.info/Bootstraptest/test.js"></script>
    </body>
</html>

Javascript

$("button#signinbutton").click(function() {
    if ($("#username").val() == "" || $("#password").val() == "") {
        $("p#result).html("Please enter both userna");
    } else {
        $.post($("#loginform").attr("action"), $("#loginform:input").serializeArray(), function(data) {
            $("p#result").html(data);
        });
        $("#loginform").submit(function() {
            return false;
        });
    }
});

php

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
ob_start();
session_start();
include 'connect.php';
//get form data
$username = addslashes(strip_tags($_POST['username']));
$password = addslashes(strip_tags($_POST['password']));
$password1 = mysqli_real_escape_string($con, $password);
$username = mysqli_real_escape_string($con, $username);
if (!$username || !$password) {
    $no = "Please enter name and password";
    echo ($no);
} else {
    //log in
    $login = mysqli_query($con, "SELECT * FROM Agent WHERE username='$username'")or die(mysqli_error());
    if (mysqli_num_rows($login) == 0)
        echo "No such user";
    else {
        while ($login_row = mysqli_fetch_assoc($login)) {

            //get database password
            $password_db = $login_row['password'];

            //encrypt form password
            $password1 = md5($password1);

            //check password
            if ($password1 != $password_db)
                echo "Incorrect Password";
            else {
                //assign session
                $_SESSION['username'] = $username;
                $_SESSION['password'] = $password1;
                header("Location: http://rentaid.info/Bootstraptest/aboutus.html");
            }
        }
    }
}
?>

Edit

$("button#signinbutton").click(function(){

    if($("#username").val() ==""||$("#password").val()=="")
$("p#result).html("Please enter both userna");
  else


    $.post ($("#loginform").attr("action"),
        $("#loginform:input").serializeArray(),
    function(data)  {
$("p#result).html(data);  });  




});
 $("#loginform").submit(function(){
    return false;
});

Upvotes: 1

Views: 123

Answers (3)

Kami
Kami

Reputation: 19447

Modify your javascript section so that

$("button#signinbutton").click(function() {
    if ($("#username").val() == "" || $("#password").val() == "") {
        $("p#result).html("Please enter both userna");
    } else {
        $.post($("#loginform").attr("action"), $("#loginform:input").serializeArray(), function(data) {
            $("p#result").html(data);
        });
    }
});

$("#loginform").submit(function() {
    return false;
});

is outside the function call.

Upvotes: 0

Khushboo
Khushboo

Reputation: 1817

First of all, Remove :-

header("Location: http://rentaid.info/Bootstraptest/aboutus.html");

and if you want to display the data, echo username and password.

$_SESSION['username'] = $username;
$_SESSION['password'] = $password1;
echo $username."<br>".;
echo $password1;

Upvotes: 2

danielvdende
danielvdende

Reputation: 710

The reason you are being redirected is that you are also calling $.submit. The classic form submit will redirect you to a new page, which is exactly what you don't want when you're using AJAX. If you remove this call:

$("#loginform").submit(function() {
    return false;
});

you probably should have working solution. If not, let me know :)

Upvotes: 1

Related Questions