Php 将检索到的值显示为未定义的AJAX
我正在使用AJAX将值发送到PHP并从PHP检索值。问题是我从PHP获得的值在AJAX中被视为未定义。请帮我解决这个问题 AJAX代码:Php 将检索到的值显示为未定义的AJAX,php,javascript,ajax,jquery,Php,Javascript,Ajax,Jquery,我正在使用AJAX将值发送到PHP并从PHP检索值。问题是我从PHP获得的值在AJAX中被视为未定义。请帮我解决这个问题 AJAX代码: var channel; function overall() { $(".one").show(); $(".two").hide(); $(".three").hide(); $(".four").hide(); window['channel'] = "OVERALL"; $.ajax({
var channel;
function overall() {
$(".one").show();
$(".two").hide();
$(".three").hide();
$(".four").hide();
window['channel'] = "OVERALL";
$.ajax({
type: "GET",
url: "dash2.php",
data: ({channel: channel}),
success: function (data) {
console.log(data.a);
console.log(data.b);
console.log(data.c);
}
});
}
PHP代码:
<?php
$channel=$_GET['channel'];
$host="192.168.0.29";
$username="root";
$password="root";
$dbname="realcl";
mysql_connect($host,$username,$password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
$query = 'select * from '.$channel;
$masterresult = mysql_query($query);
while($row1 = mysql_fetch_array($masterresult))
{
$success=$row1[1];
$timeout=$row1[2];
$fail=$row1[3];
}
echo json_encode(array("a"=>"$success","b"=>"$timeout","c"=>"$fail"));
?>
再来一次!!!无论如何,让我解释一下 首先,在ajax中通过get方法发送通道
data:({channel:channel}), //here which is just a vairable global variable
//and not assigned any value in your given code...
所以这个发送通道是空的。。。因此你的任务不起作用,因为那会变成
$query = "select * from '' "; <-- here you have a singe quote `'` which also gives error
查看您的php和查询$通道看起来像tbalename,如果它是整体的,那么您可以在ajax中传递字符串,如果不是,那么您必须在ajax中为通道分配一个表名
type:"GET" //here type is get not method
data:{channel:"OVERALL"}, //note you don't need an extra bracket here `()`
因为您正在用json编码数据 尝试在ajax中添加
dataType:“json”
,ajax具有type
方法,因此更改为type
数据应仅位于{}
中:
$.ajax({
type:"GET",
url:"dash2.php",
dataType: 'json',
data:{channel:channel},
success:function(data){
console.log(data.a);
console.log(data.b);
console.log(data.c);
}
});
试着用这句话:
$channel=$_GET['channel'];
选择数据库后:
mysql_select_db($dbname);
只有一点其他人还没有提到:您需要在此处使用双引号或串联:
'select * from $channel'; // no
"select * from $channel"; // yes
'select * from '.$channel; // yes
变量不会在单引号内解析,因此您尝试从字面上称为$channel
的表中进行选择
另外,请在
$channel
上使用某种验证,因为您拥有的非常容易受到SQL注入攻击。将数据类型
参数设置为json
$.ajax({
type:"GET",
url:"dash2.php",
dataType: 'json',
data:{channel:channel},
success:function(data){
}
});
引用文件
dataType定义您期望从服务器返回的数据类型。如果未指定,jQuery将尝试根据响应的MIME类型推断它
代码中的一些观察结果1) 正如上面一些帖子所讨论的,您的代码容易受到
SQL注入攻击
2)
mysql.*
函数已被弃用,该扩展将在将来删除。不要依赖他们。我用大写字母来强调我的观点
对于以上两点,请尝试使用或。PDO或MySQLi都可以用来保护代码免受SQL注入攻击
一篇关于如何编写代码以保护代码免受SQL注入攻击的文章
3) 将数据库配置详细信息转移到一个单独的config.php
,这样您就不必在每个文件中都输入相同的代码,您可能需要输入一个SQL
查询。请尝试此操作。
1.使用输出缓冲,以便在ajax响应中仅接收json数据
2.在ajax中提供json数据类型
<script type='text/javascript'>
overall();
function overall() {
var channel = 1;
$.ajax({
method: "GET",
url: "http://localhost",
data: ({channel: channel}),
dataType: 'json',
success: function(data) {
console.log(data.a);
console.log(data.b);
console.log(data.c);
}
});
}
</script>
尝试console.log(数据);要查看整个respond..非常糟糕的编码,容易受到SQL注入攻击,还请注意,如果从查询中检索到多行,则只输出其中一行,这是console.log输出的最后一行undefined@Akam我只需要最后一排,我怎样才能防止SQL注入中出现这种情况呢?你能简单介绍一下这件事吗?请看,你可以先创建一个表数组,比如
array('1'=>'table1')
,然后检查ajax中的值,然后用数组中定义的值替换它。我正在使用窗口为ajax代码之前的频道赋值['channel']=“总体”;那么如何发送一个空值呢?我正在使用一个全局变量,因为我想动态查询数据库中的值,并保留当前使用的查询的选项卡,所以全局变量对我来说是一个不错的选择。全局变量工作正常。我试着调试该部分的php查询没有任何问题。您是否更改了输入ajax的e方法??是的,我确实更改了,但它仍然没有帮助echo json\u编码(数组(“a”=>$success,“b”=>$timeout,“c”=>$fail));
…试试这个..检查您是否在php中获取通道值…只需echo$\u获取['channel']
…我认为问题在于双引号,而且它只返回未定义的值。哇,我错过了关于使用单引号的部分,但这仍然没有解决我的问题。我仍然得到未定义的值。我只是解决了一个难题,我没有足够的勇气尝试调试我看不到或看不到的ajax代码测试:)尝试手动转到浏览器中请求的url,查看显示的内容。如果是错误,则您知道您的PHP仍然存在问题。如果是json,则您知道问题出在您的JS上。这不是插件的问题,也不是PHP脚本的问题我尝试了所有这些,这是A的问题JAX接收从PHP脚本传递的值。可能尝试使用$.getJSON
而不是$.ajax
。然后尝试放置$\u GET[]
在db选择之后。这没有帮助。一旦在PHP中设置了一个变量,它的值就正确了?因此,即使它在mysql查询中与否,也不会有太大的区别。当我将数据类型定义为json时,它甚至不会给我未定义的值。我想当我将数据类型用作JSONI时,它就不起作用了,因为数组已被获取从DB中正确地输入d?从您的代码中,我知道我的值没有被传递到php代码中。现在我该怎么办?您正在传递变量“channel”从脚本中,请让我知道这个值是什么?我的错,这是一个小故障。它被传递到PHP脚本中,问题是我仍然无法以正确的格式接收它。尝试在通过ajax发送之前警告通道值。是的,我这样做了,我得到了通道值。这要么是数据的格式问题,即b是被派去的还是别的什么。
<script type='text/javascript'>
overall();
function overall() {
var channel = 1;
$.ajax({
method: "GET",
url: "http://localhost",
data: ({channel: channel}),
dataType: 'json',
success: function(data) {
console.log(data.a);
console.log(data.b);
console.log(data.c);
}
});
}
</script>
//empty the current contents of the output buffer
ob_end_clean();
// Turns on output buffering
ob_start();
// print output
echo json_encode(array("a" => "success", "b" => "timeout", "c" => "fail"));
// Turn off buffering and print the contents
ob_end_flush(); // Turn off buffering and print the contents
exit;