Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Ajax数据发送到php处理程序_Php_Ajax - Fatal编程技术网

将Ajax数据发送到php处理程序

将Ajax数据发送到php处理程序,php,ajax,Php,Ajax,我正在尝试使用ajax填充朋友列表。以下是我所拥有的: <script> var username = $_GET['profile_username']; $(document).ready(function(){ function ajaxfriendsdefault(){ $.ajax({ url:'includes/handlers/ajax_load_profile_friends.php',

我正在尝试使用ajax填充朋友列表。以下是我所拥有的:

<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>
对于
ajaxfriendsdefault()
,我想向朋友展示最多5个。单击查看所有好友按钮时,我希望显示所有用户的好友

问题-在页面加载时,最初的5个朋友不显示。单击查看所有朋友时,仅显示5个&我得到以下错误<代码>注意:未定义变量:中的用户名

数据用户名已定义为
var username=$\u GET['profile\u username']我确定这就是问题所在,但我不确定如何纠正。如果我在php页面上定义它,它将是
$username=$\u GET['profile\u username']作为ajax新手,我不清楚如何定义这个&send。救命!:)我也不知道为什么最初的5个朋友没有出现?不应该
$(document).ready(function(){
触发此命令?

更改此命令:

var username = $_GET['profile_username'];
为此:

var username = "<?php echo($_GET['profile_username']); ?>";
与此相反:

data:username,
在php中,我认为您需要获得如下用户名:

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 {
        $username = $_POST['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"); 
    }     
因为没有设置
$\u POST['ViewAllFriends']
,所以您没有进入
if
条件,而是进入else条件

请试试这个


如果确定,请删除重复代码,您可以使用
$username=$\u POST['username']
out
if else语句
您将变量声明为本地删除
var
var username=
make variable global,然后您可以访问函数内部。第二件事您需要首先检查您是否通过
$\u GET['profile\u username']接收变量。

您可以检查变量是否可用

<?php
if (isset($_GET['profile_username'])) {
    $username=$_GET['profile_username'];
    echo $username;
}
?> 
反而

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'"); 
        }
ajax\u load\u profile\u friends.php中,对于默认项body,应如下所示,并从
ajax\u load\u profile\u friends.php中删除
else
条件

<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>


    data:{'username':username}
    tryuse ajax“dataType”:“json”我尝试了这个,但得到了相同的响应。未定义变量:username@AlessandroMinoccheri亲爱的,他说的不是$\u POST['username'],他说的是var username=$\u GET['profile\u username'];有所进展。我之前试过这个,不知道为什么它有效(这次稍微好一点)我插入了$username=$\u POST['username'];&现在,当我单击“查看所有朋友”时,我得到了5个朋友。当页面加载时,没有朋友显示。虽然用户名没有错误,但这是一个加号。为什么$(document).ready(function()){启动查询并在页面加载时显示5个朋友。现在View All friends运行的查询限制为5?奇怪。请尝试在document.ready()内直接调用函数ajaxfriendsdefault或其他函数。谢谢;尝试了同样的操作。未定义变量:username我确实收到了$\u GET['profile\u username'];我只是附和了一下&它就在那里。php正在反向启动。奇怪。当页面加载时什么都没有发生。当我单击“查看所有朋友”时,它只显示5个朋友,其他查询。我不明白为什么ajaxfriendsdefault()未启动并返回else查询。如果设置了查看所有好友,则假设运行if查询…?查看所有好友按钮现在显示所有好友,没有错误(谢谢)。但我仍然无法使用document.ready()启动else查询最初我没有使用ajax,但是我在一个while循环中填充。我认为如果在处理程序中使用一个循环会更好。
    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);
            }
        });
      }
    
     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'"); 
            }
    
    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'"); 
            }
    
    <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>