-1

I have below ajax call that checks if a card_number exists in the customers' table or not, however, on both cases, I get the same response that it does not exist even if the card number exists. So both final messages will be "does not exist"

agent_reload.php

<script type="text/javascript">
    $(document).ready(function()
    {
        $("#card_number").change(function() 
        { 
            var card_num = $("#card_number").val();

            $.ajax({  
                type: "POST",  
                url: 'CheckUserCardNumber.php',  
                data: "card_number="+card_number,  
                success: function(msg){            
                    var Result = $.trim(msg);

                    if(Result === "does_not_exists")
                    { 
                        alert("does not exist");
                    }  
                    else  
                    {  
                        alert("exists");
                    }  
                } 
            }); 

            return false;
        });
    });
</script>

CheckCardNumber.php

<?php
include('dbconnection.php');
if(isset($_POST['card_number']))
{
    $Card_Number = $_POST['card_number'];
    $sql = "select * from customers where card_id='$Card_Number'";
    $Result = $db->query($sql);

    if ($Result->num_rows != 0)
    {
        echo "exists";
    }
    else
    {
        echo "does_not_exists";
    }
}
M. Eriksson
  • 12,711
  • 4
  • 26
  • 38
Bayar Shahab
  • 31
  • 1
  • 1
  • 6

2 Answers2

1

Looks like you set the var to card_num but pass card_number to ajax:

var card_num = $("#card_number").val();
.
.
.
data: "card_number="+card_number,
Rob Moll
  • 3,316
  • 2
  • 8
  • 15
  • Thank you so much, guys, it looks like it was a silly mistake, i had passed a wrong variable like mentioned by @Rob Moll. I have also fixed the SQL Injection. prepare("select * from customers where card_id=? "); $stmt->bind_param("s", $Card_Number); $stmt->execute(); $Result = $stmt->get_result(); if ($Result->num_rows != 0) { echo "exists"; } else { echo "does_not_exists"; } } – Bayar Shahab Sep 21 '19 at 14:15
1

A tip

A good programming practise would be to use camelcase naming and proper indenting.

// <!-- CheckCardNumber.php --> 
<?php
if (isset($_POST['card_number'])) {

  include('dbconnection.php');

  $cardNumber = $_POST['card_number'];
  $sql        = "SELECT * from customers where card_id= '$cardNumber'";
  $result     = $db->query($sql);

  echo $result->num_rows > 0 ? 'Exists' : 'Does not exist.';
}

Solution

In your ajax request, you can just alert whatever response you got from if you set a proper message.

Also, make sure the value you send with your request is correct.

            $.ajax({  
                type: "POST",  
                url: 'CheckUserCardNumber.php',  
                data: "card_number="+$(this).val(),  
                success: function(msg){            
                   alert(msg); 
                } 
            }); 

Additionally, please always use PDO/Prepared MySQLI. How can prepared statements protect from SQL injection attacks?

Community
  • 1
  • 1
  • _"A good programming practise would be to use camelcase naming"_ - I wouldn't call that _"a good programming practice"_ but rather a personal preference. – M. Eriksson Sep 18 '19 at 11:53
  • It is considered a good practise because it avoids confusion and adds consistency. I didn't say it was the best practise, but it is a good one. – Aaron NoHuanKnows Sep 18 '19 at 11:55
  • A good practice would to be consistent, that I agree with. But if you consistently use snake case, camel case or any other casing doesn't really matter. – M. Eriksson Sep 18 '19 at 11:55
  • I merely suggested one of several good practises that said poster can look into, do we really have to have an argument about how i named a matter that clearly would help the poster? – Aaron NoHuanKnows Sep 18 '19 at 11:59