从jquery运行php单击,从调用另一个文件的php文件返回数组
昨天,我设法从一个数据库中获取数据,并将其输出和存储到一个java数组中。然而,这是在加载,现在的代码不会工作的点击。 因此,我阅读了有关ajax的内容,并具有以下功能:从jquery运行php单击,从调用另一个文件的php文件返回数组,php,ajax,json,mysqli,Php,Ajax,Json,Mysqli,昨天,我设法从一个数据库中获取数据,并将其输出和存储到一个java数组中。然而,这是在加载,现在的代码不会工作的点击。 因此,我阅读了有关ajax的内容,并具有以下功能: var infArray = new Array(); var country; $('#australia').click(function() { //console.log("you clicked"+txt); country = 'Australia';
var infArray = new Array();
var country;
$('#australia').click(function() {
//console.log("you clicked"+txt);
country = 'Australia';
$.ajax({
type: 'POST',
url: 'php/Maps.php',
data: {country: country},
success: function(data){
alert("success"+data); // this will hold your $result value
infArray = JSON.parse(data)
console.log( 'Return:' + data );
}
});
});
据我所知,这将打开包含函数的php文件,并允许您使用$u POST使用变量“country”
因此,我的php文件如下所示:
<?php
require '../classes/Mysql.php';
function get_Stockist(){ // if su = 0 then stockist if = 1 then member
$mysql = new Mysql();
$result = $mysql->getInfo($_POST['country']);
echo json_encode($result);
}
但是,my html中的结果为空您从未在AJAX调用的PHP文件中调用函数
get_Stockist()
。将
get_Stockist()
添加到PHP文件以调用函数
您的另一个函数是getinfo
,没有大写字母i。所以应该是
$mysql->getinfo($\u POST['country'])
而不是$mysql->getInfo($\u POST['country'])
我将其更改为大写字母I,并删除了该方法,因此现在Maps.php看起来像
function getinfo($country){
$rows = array();
$query = "SELECT Name,add1 FROM stockistsWorld WHERE Country = '". mysql_escape_string($country) ."' LIMIT 5";
//$query = "SELECT Name,add1 FROM stockistsUK LIMIT 10";
$result = mysqli_query($this->conn, $query);
/* numeric array */
while($row = mysqli_fetch_array($result, MYSQLI_NUM)){
$rows[] = $row;
}
return $rows;
}