在jQuery.Post方法中执行mySQL查询 [英] Execution of mySQL query in jQuery.Post method

查看:101
本文介绍了在jQuery.Post方法中执行mySQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含js的javascript文件,以在下拉列表发生更改时触发事件. JS如下:

I have created a javascript file that contains js to trigger an event onchange of a drop down list. The JS is below:

// /public/js/custom.js

jQuery(function($) {
    $("#parent").change(function(event){
        event.preventDefault();
        var parentID = $('#parent').val();
        var id = $('#id').val();
        $.post("/menu/GetMenuChildren", {pID: parentID, thisID: id }, 
            function(data){
                if(data.response === true){        
                    var $el = $("#Position");
                    $el.empty(); // remove old options
                    $.each(data.newOptions, function(key, value) {
                      $el.append($("<option></option>")
                         .attr("value", value).text(key));
                    });
                } else {
                    // print error message
                    alert("something went wrong in Post");
                }
            }, 'json');

        alert("After Post");
    });
});

在Controller.php中,我有一个功能GetMenuChildrenAction,如下所示:

In my Controller.php I have an function GetMenuChildrenAction as shown below:

public function GetMenuChildrenAction()
{
    $request = $this->getRequest();
    $response = $this->getResponse();
    if ($request->isPost()) 
        {
        $post_data = $request->getPost();
        $parent_id = $post_data['pID'];
        $id = $post_data['thisID'];
        //$this->form->get('Position')->SetOptionValues(
        $newOptions = $this->getMenuTable()->GetPositionsByID($parent_id, $id);
        if(isset($newOptions))
        {
            $response->setContent(\Zend\Json\Json::encode(array('response' => true, 'newOptions'      => $newOptions)));
        }
        else
        {
            $response->setContent(\Zend\Json\Json::encode(array('response' => false)));
        }
    }
    return $response;
}

在MenuTable.php中,有一个方法GetPositionsByID,如下所示:

In the MenuTable.php there is a Method GetPositionsByID as shown below:

public function GetPositionsByID($parentID, $id)
{
    if($parentID == 0)
    {
        $menu = $this->getMenu($this->id);
        $parentID = $menu->parent_id;
    }
    if(isset($parentID))
    {
        $sql = "Select ID,Label from Menu Where parent_ID = " . $parentID . " and id > 1 and id <> " . $id . " Order by Position,Label";

        try
        {
           $statement =  $this->adapter->query($sql);
        }
        catch(Exception $e) {
            console.log('Caught exception: ',  $e->getMessage(), "\n");
        }

        $res = $statement->execute();

        $rows = array();
        $i = 0;
        foreach ($res as $row) {
            $i = $i + 1;
            $rows[$i] = array (
                $i => $row['Label']
            );
        }

        return $rows;
    }
    return array();
}

这一切似乎都正确地挂钩了,当我调试代码时,我得到了这一行:

It all seems to be hooked up correctly, when I debug the code, I get the this line:

$statement =  $this->adapter->query($sql);

,然后什么也没有发生.如果我替换了GetPositionsByID方法中的所有代码,并仅返回如下数组:

and then nothing happens. If I replace all the code in the GetPositionsByID method, and simply return an array like the following:

return array('1' => 'one', '2' => 'two');

它很好用,但是我想从数据库中获取数据.有谁知道为什么执行会在此行失败?

it works great, however i want to get the data from the DB. Does anyone know why the execute would fail on this line?

$statement =  $this->adapter->query($sql);

预先感谢

推荐答案

问题是适配器为空.

这篇关于在jQuery.Post方法中执行mySQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆