1

I am trying to implement a simple javascript program in which I am making an ajax call to a php script. I make sure that the page is not getting refreshed on its own.So,now if I am using echo functions in php then it is not working.

    $("#check").on('click',function(){
        //alert("hello");
        var user_name=document.getElementById("user_name").value;
        var pwd=document.getElementById("pwd").value;

        $.ajax({
            url:'checkUser.php',
            type:'POST',
            data:{
                user_name:user_name,
                pwd:pwd
            },
            success:function(){
                alert("hello"); //This section works fine so the php file is getting called
            }
        });

return false;

})

checkUser.php

<?php require 'database.php';
if($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
}
//Database is getting connected properly
$user_name=$_POST['user_name'];
$password=$_POST['pwd'];
echo("<script>console.log('PHP:');</script>"); //not getting displayed on console
$sql="Select * from login where User_name='$user_name' and Password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
echo $count; //not getting displayed on console
 ?>

If I want to use an echo function, then how should I do it?Thanks!

Aayushi
  • 1,736
  • 1
  • 26
  • 48
  • 5
    Your code is vulnerable to [**SQL injection attacks**](https://en.wikipedia.org/wiki/SQL_injection). You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky Apr 26 '17 at 19:30
  • 3
    Stop using the `mysql_*` functions. They have been deprecated since v5.5 (Jun 2013) and removed since v7.0 (Dec 2015). Instead use the [**mysqli_***](https://secure.php.net/manual/en/book.mysqli.php) or [**PDO**](https://secure.php.net/manual/en/book.pdo.php) functions with [**prepared statements**](https://secure.php.net/manual/en/pdo.prepare.php) and [**bound parameters**](https://secure.php.net/manual/en/pdostatement.bindparam.php). – Alex Howansky Apr 26 '17 at 19:31
  • 4
    **Never** store plain text passwords. You should use [`password_hash()`](http://us3.php.net/manual/en/function.password-hash.php) and [`password_verify()`](http://us3.php.net/manual/en/function.password-verify.php) instead. If you're using a version of PHP prior to 5.5, do **not** use MD5 or SHA1 to hash passwords. Instead you can use [this compatibility pack](https://github.com/ircmaxell/password_compat). – Alex Howansky Apr 26 '17 at 19:31
  • Woot, got to use all my blurbs in one post! – Alex Howansky Apr 26 '17 at 19:31
  • `` gets sent back to the browser as a string. The `$count;` also goes back in that same string, you aren't doing anything with the return value. – chris85 Apr 26 '17 at 19:33
  • Or better yet, use a framework. – Gerardo Apr 26 '17 at 19:35
  • @Gerardo lets teach him the basics before he starts using other things and never understand how they work. – iam-decoder Apr 26 '17 at 19:39
  • @iam-decoder Yeah, you're right. – Gerardo Apr 26 '17 at 19:41
  • what are the other methods of encryption? If I use "crypt" method,will it work fine? @AlexHowansky – Aayushi Apr 27 '17 at 10:56
  • i am a girl! :) @iam-decoder – Aayushi Apr 27 '17 at 11:25
  • It's not a question of whether it will work, it's a question of how secure it is. `crypt()` with `CRYPT_BLOWFISH` and a reasonable cost parameter is acceptable, but using `password_hash()` is much easier and automatically scales with time. – Alex Howansky Apr 27 '17 at 13:40
  • @aayushi my apologies, I should have used more general terms like "them" and "they" rather than assume your gender. Glad you got your problem worked out! – iam-decoder Apr 27 '17 at 16:30

4 Answers4

2

You are not passing the results through the success() function. You have to display the results of the response from the page. You can do this by doing:

    $("#check").on('click',function(){
        //alert("hello");
        var user_name=document.getElementById("user_name").value;
        var pwd=document.getElementById("pwd").value;

        $.ajax({
            url:'checkUser.php',
            type:'POST',
            data:{
                user_name:user_name,
                pwd:pwd
            },
            success:function(data){
                alert(data); //data will contain the echo value
            }
        });

return false;

})

The "data" variable will always contain the response that is printed on the page, including errors so make sure you take that into considering if you are using it as a validation.

Andrew Rayner
  • 1,056
  • 1
  • 6
  • 20
1

The echo command in your php file basically returns the value to the jQuery's ajax function. BUT you don't handle the response correctly.

How to use it correctly?

Add a parameter to the success event of the $.ajax function. This parameter holds the returned value from the php file.

    $.ajax({
                url:'checkUser.php',
                type:'POST',
                data:{
                    user_name:user_name,
                    pwd:pwd
                },
                success:function(data){
console.log(data);
                }
            });

And in your php file:

echo "PHP:';
Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39
1

so you want to use echo, that's fine. but what you need to understand is that it's just outputting text to a request that is only going to be read by ajax, in your ajax.success() function you need to append the string it read to the DOM the user is seeing:

$.ajax({
        url:'checkUser.php',
        type:'POST',
        data:{
            user_name:user_name,
            pwd:pwd
        },
        success:function(data){
            console.log('data');
            alert("hello"); //This section works fine so the php file is getting called
        }
    });

also keep in mind, you're echoing 2 things back to back so ajax will see something like this:

<script>console.log('PHP:');</script>15

where the 15 is your $count

its up to you on how you want to split that up for correct logging, most people use a json string.

iam-decoder
  • 2,554
  • 1
  • 13
  • 28
0

You will recieve server response in your "success" callback

success:function(data){
            alert(data); 
        }
M Melnikov
  • 83
  • 3