AJAX How do I call a PHP file and return the result?
Results 1 to 2 of 2

Thread: How do I call a PHP file and return the result?

  1. #1
    Senior Member
    Join Date
    Nov 2004
    Posts
    249

    How do I call a PHP file and return the result?

    I can't get this to work. I just want to check my database and return 1 or 0 depending on the result but nothing happens. What did I do wrong?

    Code:
    var username = $('#username').val();  
      
            //use ajax to run the check  
            $.post("check_username.php", { username: username },  
                function(result){  
                    //if the result is 1  
                    if(result == 1){  
                        //show that the username is available  
                        $('#username_availability_result').html(username + ' is Available');  
                    }else{  
                        //show that the username is NOT available  
                        $('#username_availability_result').html(username + ' is not Available');  
                    }  
            });

    PHP Code:
    //connect to database  
    mysql_connect('host''user''pass');  
    mysql_select_db('database_name');  
      
    //get the username  
    $username mysql_real_escape_string($_POST['username']);  
      
    //mysql query to select field username if it's equal to the username that we check '  
    $result mysql_query('select username from users where username = "'$username .'"');  
      
    //if number of rows fields is bigger them 0 that means it's NOT available '  
    if(mysql_num_rows($result)>0){  
        
    //and we send 0 to the ajax request  
        
    echo 0;  
    }else{  
        
    //else if it's not bigger then 0, then it's available '  
        //and we send 1 to the ajax request  
        
    echo 1;  

    To realize that you don't know so much is a
    good beginning...

  2. #2
    Senior Member
    Join Date
    Mar 2009
    Posts
    809
    If you're using Firefox I would install the Firebug add-on and check the console; it will report AJAX and JavaScript errors. Chrome has similar functionality.

    Everything at first glance seems to be okay. Things I would check if I were in your situation:

    - Check the path of the PHP script and make it's correct in relation to the JavaScript file. If "nothing happens" it's usually because there's a path issue of some kind.
    - Check the variables along the way and either echo/alert/log/etc as you go to see if values are actually being reported as you expect.

    Also, people here are going to yell at you for using the MySQL extension instead of the MySQLi or PDO extension. This is me not yelling at you. But really. Stop using it.
    Declare variables, not war.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •