Reputation: 1017
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);
}
});
});
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>';
?>
I am receiving the error
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!
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..
Upvotes: 0
Views: 179
Reputation: 3856
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']);
on res.php
also you're not sending the 'level' variable properly, you're missing an '&'
data = 'building=' + buildingVal.val() + '&level=' + levelVal.val();
Upvotes: 4