我如何处理这个ajax数据?

时间:2012-06-17 01:52:11

标签: php javascript jquery html ajax

我将如何在php中处理这个Ajax。 我想要做的是将数据发送到process.php,如果mode = loadlinks,它将执行mysql查询

function PresentLinks(div_id){

        $("#loading-status").fadeIn(900,0);
        $("#loading-status").html("<img src='img/bigLoader.gif' />");           

$.ajax({

            type: "POST",
            url: "process.php",
            data: "mode=loadlinks",

            success: function(msg){

                $("#loading-status").fadeOut(900,0);
                $("#"+div_id).html(msg);


            }

        });}

我想要处理的是

if($_POST['mode'] == loadlinks){  // this is what i want to ask
$query = "SELECT * FROM site ORDER BY link_id DESC";
$result = MYSQL_QUERY($query) or die (mysql_error());
while($data = mysql_fetch_row($result)){
echo ("$data[1]");
}}
else {
}

2 个答案:

答案 0 :(得分:2)

您需要在PHP中引用字符串。否则它们将被假定为常数。您还应该使用PDO

if($_POST['mode'] == 'loadlinks'){
    $pdo = new PDO('mysql:host=HOST;dbname=DATABASE'), 'username', 'password');
    $stmt = $pdo->execute('SELECT * FROM site ORDER BY link_id DESC');

    $sites = $stmt->fetchAll();
    foreach($sites as $site) {
        echo "<div>" . $site['name'] . "</div>"; // Or whatever info you want to output
    }
}

对于性能,您应该指定要检索的表列名称,而不是使用*

答案 1 :(得分:1)

你需要引用字符串值

  if($_POST['mode'] == 'loadlinks'){.....