1
        <?php
        $username=strip_tags($_POST['username']);
        $password=strip_tags($_POST['password']);


        $username=stripslashes($username);
        $password=stripslashes($password);

        $username=mysqli_real_escape_string($dbc,$username);
        $password=mysqli_real_escape_string($dbc,$password);

        $password=md5($password);

        $sql="SELECT * FROM mobile_users WHERE username='$username' LIMIT 1 ";
        $query=mysqli_query($dbc, $sql);
        $row=mysqli_fetch_array($query);

        //$expire=time() +60*60*24*30;
        //setcookie("");


        //}



        $id=$row['idmobile_users'];

        $db_password=$row['password'];
          $_SESSION['error'] = "could not connect";

        if($password==$db_password){
        $_SESSION['username']=$username;
        $_SESSION['id']=$id;
        $_SESSION['last_time']=time();

        header("Location: apply_now.php");

        }
        else {


            echo 'Invalid username or password';

        }


        }

        ?>
    <script>
$(function(){
$(document).on("click", ".button", function(e) {
e.preventDefault();
{       
//This will capture all the inputs from the form
var infom = $("#myform").serialize();
        //$("#showresult").addClass('loader');                
        $.ajax({            
            beforeSend: function() { },         
            type: "POST",
            url: "login.php",
            data:infom,          
            success: function(result){      
            //$("#showresult").removeClass('loader');
            $('#showresult').html(result);
            }
        });     
        e.preventDefault(); 
}
}); 
});
</script>

ok so i want it to place the error message from the php code where the id is in html, the id is in a span tag. Tried alot of ways but cant seem to get it working properly. I hope somone can help me resolve this issue. Thanks

3
  • Are the codes posted above from same page? Or PHP code is in another page? Commented Aug 19, 2016 at 5:34
  • @ krushio vida the php and html is on the same page but the ajax is in its own file Commented Aug 19, 2016 at 5:35
  • In your php page $sql="SELECT * FROM mobile_users WHERE username='$username' LIMIT 1 "; you are not checking the password? out curiosity Commented Aug 19, 2016 at 6:20

2 Answers 2

1

First, Ajax code will not understand if the response from php page if it is an error or a success response, What you do just return the response but specify if it is an error or not in the PHP page.

It would be better if you separate your php page from your html page.

This should be in your html page.

 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.js"></script>
<script>
$(function(){
$(document).on("click", ".button", function(e) {
e.preventDefault();
{       
//This will capture all the inputs from the form
var infom = $("#myform").serialize();
        $("#showresult").addClass('loader');                
        $.ajax({            
            beforeSend: function() { },         
            type: "POST",
            url: "login.php",
            data:infom,          
            success: function(result){      
            $("#showresult").removeClass('loader');
            $('#showresult').html(result);
            }
        });     
        e.preventDefault(); 
}
}); 
});
</script>
    <style>
    .loader{
        background-image:url(http://loading.io/assets/img/default-loader.gif);
        background-repeat:no-repeat;
        background-position:center;
        height:100px;
        background-size: 30px;
    }
    </style>
<div id="showresult"></div>

<form id="myform">
<!--your inputs-->
<input name="username" type="text"><br>
<input name="password" type="text"><br>
<button class="button"> submit </button>
</form>

And this on your php page

   <?php

    //YOUR PHP CODES....

    header("Location: apply_now.php");
    // i would prefer something like
    echo ' welcome '.$username.' '; 
    echo "<script> window.location.href='apply_now.php';</script>";

    }
    else {


        echo 'Invalid username or password';

    }
    echo json_encode(array('error' => $error, 'message' => $message, 'redirect' => $redirect));


    ?>
Sign up to request clarification or add additional context in comments.

18 Comments

ok i placed the php code in its own file but when i submit now i get a blank page.
@ShannaChambers Its like your Ajax code is not working correctly then. Should i give you another ajax code that will work for you?
@ShannaChambers I have provided the alternative
its not working it just stays on the page and not doing anything
its working now i just removed the if(isset($_POST['login'])
|
0

I dont understand exactly what you mean but when i look at the code could it be that youre page is just refreshing afther the submit? If im correct the form will reload the page on a submit if you dont use e.preventDefault(). If it is reloading just include the prevent default, the rest of youre code looks ok

6 Comments

could you check through the code again its not working even after i use the e.preventDefault();
Do you see the console log afther submitting it?
its not showing on the html page nothing or should i go into browser inspector to see it
If you are using chrome you could use the developer tools by hitting f12 otherwise just change it to alert(response);
its responding the entire html page except the section with the php code.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.