Q:

Sending Ajax data to Handler php

I'm trying to use ajax to populate a friends list.  Here is what I have:
<script>
    var username = $_GET['profile_username'];

    $(document).ready(function(){

    function ajaxfriendsdefault(){

        $.ajax({

            url:'includes/handlers/ajax_load_profile_friends.php',
            type:'POST',
            data:username,

            success: function(data) {
                $('#data_friends').html(data);
            }
        });
    }
 });

    function ajaxfriends(){

        $.ajax({

            url:'includes/handlers/ajax_load_profile_friends.php',
            type:'POST',
            data:username,

            success: function(data) {
                $('#data_friends').html(data);
            }
        });
      }

</script>

  <div class="panel-body">
    <ul id="data_friends"></ul>
    <div class="clearfix"></div>
    <input class="btn btn-default" type="button" id="ViewAllFriends" name="ViewAllFriends" value="View All Friends" onclick="ajaxfriends()">
  </div>

I'm sending this to the following handler ajax_load_profile_friends.php  I have here the following:
    global $con;

    if(isset($_POST['ViewAllFriends'])) {
        $username = $_POST['username'];
        //Query to run if button ViewAllFriends submitted
        $query = mysqli_query($con,"SELECT * FROM users WHERE friend_array LIKE '$username,%' OR friend_array LIKE '%,$username,%' OR friend_array LIKE '%,$username'"); 
    } else {
        //Default query limits results to 5
        $query = mysqli_query($con,"SELECT * FROM users WHERE friend_array LIKE '$username,%' OR friend_array LIKE '%,$username,%' OR friend_array LIKE '%,$username' LIMIT 0,5"); 
    }           

        while ($row = mysqli_fetch_array($query)) {
        $row['profile_pic']; 
        $row['username'];

        echo "<li><a class='thumbnail pull-left' href='" . $row['username'] . "'>
                <img src='" . $row['profile_pic'] . "' title='" . $row['username'] . "'>
              </a></li>";

    }

For ajaxfriendsdefault() I'd like to show friends to a limit of 5.  When the View All Friends button is clicked, I'd like to display all of the users friends.  
The problems - on page load the initial 5 friends do not display.  When View All Friends is clicked, only 5 display & I'm getting the following error.  Notice: Undefined variable: username in 
The data username has been defined as var username = $_GET['profile_username'];  I'm certain this is the problem, but am unsure on how to correct.  If I were to define it on the page in php it would be $username = $_GET['profile_username'];  Being new to ajax I'm unclear on how to define this & send.  Help! :) I'm also not sure why the initial 5 friends aren't showing?  Shouldn't $(document).ready(function(){ fire this?

A:

you declared variable as locally remove var  from var username = <?php echo $username; ?> make variable global then you can access inside the function. second thing you need to  check first you are receiving variable or not through $_GET['profile_username'];
you can check your variable is available or not 
<?php
if (isset($_GET['profile_username'])) {
    $username=$_GET['profile_username'];
    echo $username;
}
?> 

then you can try like this 
function ajaxfriends(){

    $.ajax({

        url:'includes/handlers/ajax_load_profile_friends.php',
        type:'POST',
        data: { 
            'username': <?php echo $username; ?>
        },

        success: function(data) {
            $('#data_friends').html(data);
        }
    });
  }

and you need to change 
 if(isset($_POST['username'])) {
            $username = $_POST['username'];
            //Query to run if button ViewAllFriends submitted
            $query = mysqli_query($con,"SELECT * FROM users WHERE friend_array LIKE '$username,%' OR friend_array LIKE '%,$username,%' OR friend_array LIKE '%,$username'"); 
        }

instead 
if(isset($_POST['ViewAllFriends'])) {
            $username = $_POST['username'];
            //Query to run if button ViewAllFriends submitted
            $query = mysqli_query($con,"SELECT * FROM users WHERE friend_array LIKE '$username,%' OR friend_array LIKE '%,$username,%' OR friend_array LIKE '%,$username'"); 
        }

in  ajax_load_profile_friends.php and for default item body should be like this and remove else condition from  ajax_load_profile_friends.php
<div class="panel-body">
    <ul id="data_friends">
        <?php 
        if (isset($_GET['profile_username'])) {
            $username=$_GET['profile_username'];

        //Default query limits results to 5
    $query = mysqli_query($con,"SELECT * FROM users WHERE friend_array LIKE '$username,%' OR friend_array LIKE '%,$username,%' OR friend_array LIKE '%,$username' LIMIT 0,5"); 
        while ($row = mysqli_fetch_array($query)) {
        $row['profile_pic']; 
        $row['username'];

        echo "<li><a class='thumbnail pull-left' href='" . $row['username'] . "'>
                <img src='" . $row['profile_pic'] . "' title='" . $row['username'] . "'>
              </a></li>";

    }
}
  ?>
    </ul>
    <div class="clearfix"></div>
    <input class="btn btn-default" type="button" id="ViewAllFriends" name="ViewAllFriends" value="View All Friends" onclick="ajaxfriends()">
  </div>

