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']);

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

报告相同问题?

悬赏问题

  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了