3

我正在使用 AJAX 向 PHP 发送值并从 PHP 中检索值。问题是我从 PHP 获得的值在 AJAX 中被视为未定义。请帮我解决这个问题。

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"));

?>
4

5 回答 5

3

只是其他人尚未涵盖的一点:您需要在此处使用双引号或连接:

'select * from $channel'; // no
"select * from $channel"; // yes
'select * from '.$channel; // yes

变量不会在单引号内解析,因此您尝试从字面上称为$channel.

另外请使用某种验证,$channel因为您所拥有的非常容易受到 SQL 注入攻击。

于 2013-03-27T12:09:19.787 回答
0

请试试这个。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>

php代码

   //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;
于 2013-03-27T12:48:10.533 回答
0

再次!!!不管怎样,让我解释一下……

首先,您通过 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

其次..ajax 具有类型属性而不是方法..

 type:"GET" //here type is get not method

看到你的 php 和查询 .. $channel 看起来像一个 tbalename ,如果它是 OVERALL ,那么你可以在你的 ajax 中传递字符串,如果没有,那么你必须在 ajax 中为通道分配一个表名

 type:"GET" //here type is get not method
 data:{channel:"OVERALL"}, //note you don't need an extra bracket here `()`
于 2013-03-27T12:05:44.707 回答
0

因为您正在将数据编码为 json。

尝试添加dataType:"json"您的 ajax 并且 ajaxtype没有方法,因此对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'];

db选择后:

mysql_select_db($dbname);
于 2013-03-27T12:06:04.467 回答
0

dataType参数设置为json

$.ajax({
   type:"GET",
   url:"dash2.php",
   dataType: 'json',
   data:{channel:channel},
   success:function(data){
   }
 });

引用jQuery.ajax文档

dataType 定义您期望从服务器返回的数据类型。如果没有指定,jQuery 将尝试根据响应的 MIME 类型推断它。

从您的代码中很少观察到。
1) 正如上面一些帖子中所讨论的,您的代码容易受到SQL injection attacks.
2)mysql_*功能已被弃用,扩展将在未来被删除。不要依赖它们。我用大写字母来强调我的观点。

对于上述两点,请尝试使用PDOMySQLi。PDO 或 MySQLi 都可以用来保护您的代码免受 SQL 注入攻击。

这是一篇关于如何编写代码来保护您的代码免受 SQL 注入攻击的帖子。

3)将您的数据库配置详细信息转移到一个单独的config.php位置,这样您就不必在每个文件中放入相同的代码,您想要放入SQL查询。

于 2013-03-27T12:23:42.980 回答