0

I've this code snippet for ajax request:

<script type="text/javascript">
        $(document).ready(function() {
            $('#temp').click(function() {
                var username = $('#un').val();
                $.ajax({
                    url: "inter.php",
                    data: 'user=' + username,
                    cache: false,
                    type: 'GET',
                    success: function(data, textStatus, jqXHR) {
                        if (! data.length === 0) {

                            $('#msg').append('available');
                            $('#sub').removeAttr('disabled');
                        } else {

                            $('#msg').append("Username not available");
                            $('#sub').attr('disabled', 'true');
                        }
                    }
                });
            });
        });
    </script>

and the code in inter.php

<?php

$arr = array('one', 'two', 'three');
if (in_array($_GET['user'], $arr)) {
    echo "Username available";
} else {
    //echo "0";
}
?>

But, every time I'm getting the message Username not available.
Can anybody help? I'm newbie to ajax.

EDIT
$_GET['user']
But the same problem remains.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
  • You're passing data in `user`, while getting it by `$_GET['un']`. – Rikesh Jul 15 '13 at 11:06
  • how can i check whether my request is handled properly or not? I'd used `data.length === 0` in script to check. Is there any other method? – Mohammad Faisal Jul 15 '13 at 11:16
  • You can have `error` function same as success which get called if any error in ajax request call, but if you getting data or not the way you're doing seems fine. Btw where you've written `Username not available` ? We can't find in your posted code. – Rikesh Jul 15 '13 at 11:19

3 Answers3

2

The problem:

data: 'user=' + username,
$_GET['un']

You're not using the same parameter-name for "user name" or whatever. In the JS you're calling it "user", in PHP you're looking for "un".

Thomas W
  • 13,940
  • 4
  • 58
  • 76
  • You need to log & debug your AJAX and PHP, because there are several points the error could be occurring at -- and without information, you won't know. Try putting in some `console.log()` to verify the AJAX response & some logging in your PHP. http://stackoverflow.com/questions/4323411/php-write-to-console – Thomas W Jul 15 '13 at 11:17
  • 1
    thanks for helping me out. The problem was with `if(!data.length === 0)` – Mohammad Faisal Jul 15 '13 at 11:37
1

You're sending ?user=someName but looking for $_GET['un']. Change it to $_GET['user']

  • I've changed `$_GET['user']` but still I'm getting `Username not available` – Mohammad Faisal Jul 15 '13 at 11:12
  • Are you sure that the username is correct? Trace the network traffic with Firebug. That will tell you what data is being sent to your server, and what the response is. That should help you debug things. –  Jul 15 '13 at 11:22
0

To debug the code I'd added line:

alert(data + " " + data.length);

after

success: function(data, textStatus, jqXHR) {

and found that I'm getting the proper message from my php file.
Then I found that the problem is with my handler

if(!data.length === 0){

where ! operator is working on only data.length whether I need it to work on whole comparison.
I'd changed it to

if(!(data.length === 0)){

and now it works.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117