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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵