using jquery to post data in zend framework

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 -webbooks.phtml ( is actually a menu and in fact a view where you type the product that you are searching for) -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) -index.phtml (this is the view space where the result should be viewed.)

This is the array that I get:

{"book_title":"Bioethics in the 21st Century",

I can see this array when I


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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

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

      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()

In IndexController the ajaxAction() should get request:

public function ajaxAction(){

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


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


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

Solution 2

If the result of your client-side call is just a redirect to another page, then why not do it all on a single controller/action/viewscript, as follows:

  1. Make the form submit a GET request rather than a POST
  2. Submit the form back to the same page, perform your remote API call, and render the results.

Even if you want to do it in two actions – one to show the form, another to display the results – I don’t see what value you are getting from the AJAX call.

Am I missing some other requirement?

Doing it with the current structure, you’d have to save the results of the remote API call into the session and then retrieve it after the redirect. Do-able, but it strikes me as unnecessary if it can be done in a single action.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply