我需要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
答案 0 :(得分:0)
此代码尝试从URL中读取$_GET['id']
值:
/superadmin/box_utilizadores_ajax.php
但此是您要求的网址:
id
如您所见,没有/superadmin/box_utilizadores_ajax.php?id=123
值(或任何其他值)。这看起来像这样:
$_GET
该值必须位于网址上,以便index.php
能够阅读。
现在,您当前正在查看的页面可能在您之前请求的网址中具有该值。但是服务器端代码不会查看您的屏幕或与您的Web浏览器进行交互。它只知道您发送的请求。该请求不包含该值。
您可以在加载页面时将该值放在请求上。在$_GET['id']
中读取"/superadmin/box_utilizadores_ajax.php?id=" . $_GET['id'] . "\", \"GET\")"
值并将其输出到发出该AJAX请求的JavaScript代码。 技术上它可能就像这样简单,只是为了证明:
// Run correctly
function loadData(){alert("called");}
tab=$("#tabID").DataTable({ajax:{url:"a.php",data:loadData}}); // The "loadData" is called
...
$("#tabID").DataTable().ajax.reload(); // The "loadData" is called again
然而 ,请注意输出原始用户输入到页面的危险。这导致XSS vulnerabilities之类的事情。请注意您输出到页面的内容,但最终需要在某处输出该值,以便JavaScript代码将其发送到下一个(AJAX)请求。 (或者,您可以将文件服务器端存储在会话状态或类似的东西中。从而完全从等式中删除URL。无论哪种方式都有利弊。)
简而言之,如果该URL的页面将读取该值,则需要在所请求的URL中包含该值。代码只能读取那里的值。