使用jquery在zend框架中发布数据 [英] using jquery to post data in zend framework

查看:84
本文介绍了使用jquery在zend框架中发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您检查出以下内容,我可能会遇到一些问题,那么我有一种使用jQuery函数的表单(webbooks.phtml)

If you check it out I have a bit of a problem with the following, I have a form (webbooks.phtml) in which I use a jQuery function

http://pastebin.com/7Pbd43fC -webbooks.phtml(实际上是菜单,实际上是查看您在哪里键入要搜索的产品)

http://pastebin.com/7Pbd43fC -webbooks.phtml ( is actually a menu and in fact a view where you type the product that you are searching for)

http://pastebin.com/q8RJWdb7 -webbookscontroller(这是一个控制器,使用API​​来根据webbooks.phtml给出的字符串/数字...等从SQL数据库中获取数据

http://pastebin.com/q8RJWdb7 -webbookscontroller ( this is a controller, which uses an API to get the data out of an SQL data base based on the string/number...etc given by the webbooks.phtml)

http://pastebin.com/vuy9GUvP -index.phtml(这是结果所在的视图空间应该被查看.)

http://pastebin.com/vuy9GUvP -index.phtml (this is the view space where the result should be viewed.)

这是我得到的数组:

{"book_title":"Bioethics in the 21st Century",
"id":"1424",
"isbn":"978-953-307-270-8","
unix_name":"bioethics-in-the-21st-century",
"visible_online":"1"} 

我可以在

die((json_encode)$result);

,我希望此数组进入我的视图(index.phtml)? 我是PHP的新手,我正在尝试做一些不好的实践,而且很可能是不可能的.我基本上只是一起学习一些东西,以测试我的知识,看看PHP可以做什么.这可能吗?

and I want this array to get to my view (index.phtml)? I am new to PHP and I'm trying to do something that may be bad practice and may well be impossible. I'm basically just hacking something together to test my knowledge and see what PHP can do. Is this possible?

推荐答案

这是使用ajax/json调用Zend Controller并获得对相同phtml的响应的基本用法示例, 因此您可以在代码中使用它.

This is an example of basic usage of calling Zend Controller with ajax/json and get response to the same phtml, so you can use it in your code.

在.phtml文件中,我有一个JavaScript(在IndexController中)调用动作ajaxAction():

In .phtml file I have javascript which call (in IndexController) the action ajaxAction():

<script language = "Javascript">
var param1 = 'first';  //or get value from some DOM element
var param2 = 'second'; //or get value from some DOM element

jQuery.ajax({
      url: '/default/index/ajax',
      type: 'POST',
      data: {param1: param1, param2:param2 },
      dataType: "json",
      success: function(result){
            var return1 = result.return1;
            var return2 = result.return2;
            // return1 and return2 is value from php file.
            // fill out DOM element or do the windows.location()
      }
});
</script>

在IndexController中,ajaxAction()应该获得请求:

In IndexController the ajaxAction() should get request:

public function ajaxAction(){
    $this->view->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender(true);

    $param1 = $this->_request->getParam('param1');
    $param2 = $this->_request->getParam('param2');

    // DO THE OTHER STUFF AND LOGIC HERE

    $results = array(
        'return1' => 'value1',
        'return2' => 'value2'
    );

    $this->_response->setBody(json_encode($results));
}

无论如何,我建议听@jakenoble并查看Zend中的(学习)上下文切换.

In any way I suggest to listen the @jakenoble and look at(learn) Context Switching in Zend.

这篇关于使用jquery在zend框架中发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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