0

我正在使用 jquery 和 ajax 调用 php 页面 res.php。代码是: -

$('#submit_button').click(function () {        
    buildingVal = $("#building");
    levelVal = $("#level");
    data = 'building=' + buildingVal.val() + 'level=' + levelVal.val();
    $.ajax( {

        url: "res.php", 
        type: "POST",
        data: data,     
        success: function (data) {
            $('#npc').html(data);
        }
    });​
});

res.php 页面代码是:-

<?php
//connect to the database
$con = mysql_connect("localhost","root","12345") or die("error ".mysql_error());

//connect to the travian table
mysql_select_db("trav",$con) or die("error ".mysql_error());

$building = mysql_real_escape_string($_GET['building']);
$level = mysql_real_escape_string($_GET['level']);


$query = "select * from ";
$query = $query . $building;
$query = $query . "where lvl=" . $level;
$query = $query . ";";
$result = mysql_query($query) or die('Error in Child Table!');

$data = mysql_fetch_assoc($result);

echo '<table><tr><td>Lumber=$data["lumber"]</td><td>Clay=$data["clay"]</td><td>Iron=$data["iron"]</td><td>Crop=$data["crop"]</td>';

?>

我收到错误

Notice: Undefined index: building in C:\xampp\htdocs\debal\res.php on line 8

Notice: Undefined index: level in C:\xampp\htdocs\debal\res.php on line 9
Error in Child Table!

我如何提取发送到页面的两个参数并在 sql 查询中使用它们从数据库中的表中检索数据。请问你能帮我吗..

4

1 回答 1

4

问题是您通过 AJAX 发送 $_POST 值并尝试在 res.php 页面上从 $_GET 分配变量。将 AJAX 函数更改为type: "GET", 或

$building = mysql_real_escape_string($_POST['building']);
$level = mysql_real_escape_string($_POST['level']);

在 res.php 上

你也没有正确发送“级别”变量,你错过了一个“&”

data = 'building=' + buildingVal.val() + '&level=' + levelVal.val();
于 2012-08-31T13:08:16.083 回答