Php 使用jquery ajax从链接获取数据

Php 使用jquery ajax从链接获取数据,php,jquery,ajax,Php,Jquery,Ajax,我有以下链接 www.example.com/profile.php?u=aaron 我希望使用ajax获取u=,以响应在我的数据库中以名称aron保存的详细信息。但当我试图用ajax代码实现这一点时,我最终得到的结果是一个空白页面 我的AJAX代码 $(document).ready(function() { $.ajax({ type: "GET", url: "fetch.php", data: "u

我有以下链接

www.example.com/profile.php?u=aaron

我希望使用ajax获取
u=
,以响应在我的数据库中以名称
aron
保存的详细信息。但当我试图用ajax代码实现这一点时,我最终得到的结果是一个空白页面

我的AJAX代码

$(document).ready(function() {
    $.ajax({    
        type: "GET",
        url: "fetch.php",             
        data: "u=":usr,   
        success: function(d) {                    
             $("#detail").html(d); 
        }
    });
});
<?php  
    // connect to db
    include 'db.php';

    if (isset($_GET['usr'])) {
        $user_query = "SELECT details FROM users_det WHERE name = ?";
        if ($stmt = mysqli_prepare($db_var, $user_query)) {
            mysqli_stmt_bind_param($stmt, "s", $_GET['usr']);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt, $details);
            while (mysqli_stmt_fetch($stmt)) {
                // fetch results
            }

            // close statement
            mysqli_stmt_close($stmt);
        }

        // echo user details
        echo $details;
    }
?>
<div id="detail"></div>
  <?php 
    $user = preg_replace('#[^a-zA-Z0-9_.]#i','',$_GET['u']); 
    ?>
fetch.php

$(document).ready(function() {
    $.ajax({    
        type: "GET",
        url: "fetch.php",             
        data: "u=":usr,   
        success: function(d) {                    
             $("#detail").html(d); 
        }
    });
});
<?php  
    // connect to db
    include 'db.php';

    if (isset($_GET['usr'])) {
        $user_query = "SELECT details FROM users_det WHERE name = ?";
        if ($stmt = mysqli_prepare($db_var, $user_query)) {
            mysqli_stmt_bind_param($stmt, "s", $_GET['usr']);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt, $details);
            while (mysqli_stmt_fetch($stmt)) {
                // fetch results
            }

            // close statement
            mysqli_stmt_close($stmt);
        }

        // echo user details
        echo $details;
    }
?>
<div id="detail"></div>
  <?php 
    $user = preg_replace('#[^a-zA-Z0-9_.]#i','',$_GET['u']); 
    ?>

我的HTML代码

$(document).ready(function() {
    $.ajax({    
        type: "GET",
        url: "fetch.php",             
        data: "u=":usr,   
        success: function(d) {                    
             $("#detail").html(d); 
        }
    });
});
<?php  
    // connect to db
    include 'db.php';

    if (isset($_GET['usr'])) {
        $user_query = "SELECT details FROM users_det WHERE name = ?";
        if ($stmt = mysqli_prepare($db_var, $user_query)) {
            mysqli_stmt_bind_param($stmt, "s", $_GET['usr']);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt, $details);
            while (mysqli_stmt_fetch($stmt)) {
                // fetch results
            }

            // close statement
            mysqli_stmt_close($stmt);
        }

        // echo user details
        echo $details;
    }
?>
<div id="detail"></div>
  <?php 
    $user = preg_replace('#[^a-zA-Z0-9_.]#i','',$_GET['u']); 
    ?>

和我的HTML代码中的以下PHP代码

$(document).ready(function() {
    $.ajax({    
        type: "GET",
        url: "fetch.php",             
        data: "u=":usr,   
        success: function(d) {                    
             $("#detail").html(d); 
        }
    });
});
<?php  
    // connect to db
    include 'db.php';

    if (isset($_GET['usr'])) {
        $user_query = "SELECT details FROM users_det WHERE name = ?";
        if ($stmt = mysqli_prepare($db_var, $user_query)) {
            mysqli_stmt_bind_param($stmt, "s", $_GET['usr']);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt, $details);
            while (mysqli_stmt_fetch($stmt)) {
                // fetch results
            }

            // close statement
            mysqli_stmt_close($stmt);
        }

        // echo user details
        echo $details;
    }
?>
<div id="detail"></div>
  <?php 
    $user = preg_replace('#[^a-zA-Z0-9_.]#i','',$_GET['u']); 
    ?>


我想知道为什么ajax没有从链接中获取名称。

首先使用window.location.href获取完整url,实例化url对象,然后通过javascript中的searchParams获取其参数

脚本:

var urlString = window.location.href; //www.example.com/profile.php?u=aaron
var url = new URL(urlString);
var usr = url.searchParams.get("u");

$(document).ready(function() {
    $.ajax({    
        type: "GET",
        url: "fetch.php",             
        data: "u="+usr,   
        success: function(d) {                    
             $("#detail").html(d); 
        }
    });
});
PHP:


您的代码中有多个错误

首先,您应该使用
数据:{u:usr},
而不是
数据:“u=”:usr,

$(document).ready(function() {
    $.ajax({    
        type: "GET",
        url: "fetch.php",             
        data: {u: usr},   
        success: function(d) {                    
             $("#detail").html(d); 
        }
    });
});
第二,在
fetch.php
文件中,您应该获取参数
$\u get['u']
,而不是
$\u get['usr']

<?php  
    // connect to db
    include 'db.php';

    if (isset($_GET['u'])) {
        $user_query = "SELECT details FROM users_det WHERE name = ?";
        if ($stmt = mysqli_prepare($db_var, $user_query)) {
            mysqli_stmt_bind_param($stmt, "s", $_GET['u']); //also change `usr` in this line
            mysqli_stmt_execute($stmt);
            mysqli_stmt_bind_result($stmt, $details);
            while (mysqli_stmt_fetch($stmt)) {
                // fetch results
            }

            // close statement
            mysqli_stmt_close($stmt);
        }

        // echo user details
        echo $details;
    }
?>


对于调试,您可以在ajax成功响应中使用
console.log(d)

检查控制台作为
data:“u=”:usr,
是语法错误。使用
data:“u=“+usr,
data:{u:usr},
仍然是空的。但是我的浏览器控制台中出现了一个错误,错误是
ReferenceError:usr未定义
@rorymcrossant这意味着
usr
未定义,或者不在您尝试使用它的范围内。你能把问题编辑成包含这些代码吗?我刚刚在html代码中添加了php代码。这就是我所有的代码@rorymcrossanmy ajax代码仍然没有得到
u=
。它在我的php代码中提供了一个错误,因为php代码无法
$\u获取['usr']。在php中,它应该是$_GET['u'];是的,我现在在控制台日志中没有错误,但是ajax并没有首先获得
u
console.log(d)。您在php代码中正确定义了$details了吗并验证是否确实获得了值。如果你得到了这个值,这意味着你的$details根本不包含任何数据。我得到了错误表单console log
TypeError:“steppup”调用了一个没有实现接口HTMLInputElement的对象。
remove
$(“#detail”).html(d)此行并写入
console.log(d)
并检查控制台日志中的输出