问题描述
我正在使用jquery和ajax调用php页面res.php.代码是:-
i am calling a php page res.php using jquery and ajax. The code is:-
$('#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页面代码为:-
the res.php page codes are:-
<?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查询中使用它们从数据库表中检索数据.请您能帮帮我.
How am i to extract both the parameters that were sent to the page and use them in the sql query to retrieve the data from the table in the database.Please could you help me..
推荐答案
问题是您正在通过AJAX发送$ _POST值,并尝试在res.php页面上从$ _GET分配变量.将AJAX函数更改为type: "GET"
或
The problem is you are sending $_POST values through AJAX and trying to assign variables from $_GET on your res.php page. Change either AJAX function to type: "GET"
, or
$building = mysql_real_escape_string($_POST['building']);
$level = mysql_real_escape_string($_POST['level']);
在res.php上
您还没有正确发送'level'变量,缺少了'&'
also you're not sending the 'level' variable properly, you're missing an '&'
data = 'building=' + buildingVal.val() + '&level=' + levelVal.val();
这篇关于在ajax函数调用的php页面中获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!