dongleibeng5602 2013-08-03 13:48
浏览 62
已采纳

从jquery点击运行php,从php文件返回数组,调用另一个文件

yesterday i managed to get my data from a database outputting and storing to a java array. However that was on load, now that code wont work for on click. So I have read about ajax and have this function:

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

By my understanding this opens the php file containing the function and allows you to use the variable "country" by using $_POST.

So my php file looks like this :

<?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);
}

so again in my eyes, $result is set to the result of the method : in Mysql.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;
}

However the result in my html is null

  • 写回答

1条回答 默认 最新

  • douzhizao0270 2013-08-03 14:04
    关注

    You never call your function get_Stockist() in your PHP file that gets called by AJAX.
    Add get_Stockist() to your PHP file to call your function.

    And your other function is getinfo, without capital i.
    So it would be $mysql->getinfo($_POST['country']); instead of $mysql->getInfo($_POST['country']);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nrf52810-c三个a 程序
  • ¥15 lego-loam跑出来的roll误差很大
  • ¥50 求一个半透明没有锯齿的圆角窗体的实现例子
  • ¥15 STM32cubeMX里的FreeRTOS无法释放内存
  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对