acquire parameters in a php page which is called by a ajax function

前提是你 提交于 2019-12-12 00:19:14

问题


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..


回答1:


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


来源:https://stackoverflow.com/questions/12215960/acquire-parameters-in-a-php-page-which-is-called-by-a-ajax-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!