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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失