获取url形式的id ajax调用php


get id in url form ajax call php

我需要url中的id来进行mysql_query。问题是,我需要通过Ajax调用来实现这一点,而$_GET['id']显然不起作用。有没有一种简单的方法可以让自己摆脱困境?谢谢:)

这是我的ajax调用:

echo "<div id='loading_utilizadores' class='loading'><img src='".$CONF['HOME']."/images/structure/ajax-loader.gif'/></div>";
        echo "<div id='utilizadores'></div>";
        echo "<script type='text/javascript'>";
            echo "CarregaAjax('"#utilizadores'",'"#loading_utilizadores'",'"".$CONF['HOME']."/superadmin/box_utilizadores_ajax.php'", '"GET'")";
        echo "</script>";

ajax函数:

function CarregaAjax(id,loading,page,method){
if(method=="GET"){
    $(document).ready(function(){
        //$(loading).ajaxStart(function(){
            $(loading).show();
            $(id).hide();
        //});
        $(id).load(page);
        $(loading).ajaxStop(function(){
            $(loading).hide();
            $(id).show();
        });
    });
}
else{
    $(document).ready(function(){
        //$(method).submit(function() {
            $(loading).show();
            $(id).load(page,$(method).serializeArray());
            $(loading).hide();
            return false;
        //});
    });
}

以及和平的html ajax调用。在这个页面中,我尝试制作$_GET['id'],但没有成功。

if (isset($_GET['id']))
{
    $officeID =  intval($_GET['id']);
}
else
{
    $officeID =  0;
}
if(!isset($crm_users))$crm_users = new crm_utilizadores;
//$officeID = 12;
$resultGetUsers = $crm_users->getUsersByOfficeId($officeID);
$html = "<table class='table1' width='100%' cellpadding='5' cellspacing='1' border='0'>";
if(!empty($resultGetUsers)){
    $html .= "<tr>";
        $html .= "<td class='table_title1'>Utilizador</td>";
        $html .= "<td class='table_title1'>Telefone</td>";
        $html .= "<td class='table_title1'>Telemóvel</td>";
        $html .= "<td class='table_title1'>E-mail</td>";
        $html .= "<td class='table_title1'>Situação</td>";
    $html .= "</tr>";
}else{
    $html .= "<tr><td class='empty1'>não foram encontrados utilizadores registados neste cliente</td></tr>";
}
//finalizar a tabela
$html .= "</table>";

我想我误解了这一点,对吧?:p

此代码尝试从URL:读取id

$_GET['id']

但是这个是您请求的URL:

/superadmin/box_utilizadores_ajax.php

正如您所看到的,没有id值(或任何其他值)。相反,它看起来像这样:

/superadmin/box_utilizadores_ajax.php?id=123

该值必须在URL上,以便$_GET读取。


现在,您当前正在查看的页面可能在之前请求的URL中具有该值。但服务器端代码并不是看着你的屏幕,也不是与你的网络浏览器交互。它所知道的只是你发送的请求,而该请求不包含该值。

加载页面时,您可以在请求中输入该值。在index.php中,读取$_GET['id']值并将其输出到发出AJAX请求的JavaScript代码技术上它可以是这样简单的东西,只是为了证明:

"/superadmin/box_utilizadores_ajax.php?id=" . $_GET['id'] . "'", '"GET'")"

但是请注意将原始用户输入输出到页面的危险。这会导致XSS漏洞。请注意您向页面输出的内容,但最终您需要在某个地方输出该值,以便JavaScript代码将其发送到下一个(AJAX)请求。(或者,也可以将文件存储在服务器端的会话状态或类似状态。从而完全从等式中删除URL。无论哪种方式都有利弊。)


简而言之,如果请求的URL上的页面要读取该值,则需要在该URL上包含该值。代码只能读取存在的值。