2

I am trying to work with jQuery in Zend Framework. And the use case I am facing problem is when I am trying to save data to the db. Always receiving ajax error though the data is being saved in the database.

The controller that I am using to add data is like below:

public function addAction()
{
    // action body
    $form = new Application_Form_Costs();
    $form->submit->setLabel('Add');
    $this->view->form = $form;

    if($this->getRequest()->isPost())
    {
        $formData = $this->getRequest()->getPost();
        {
            if ($form->isValid($formData))
            {
                $costTitle = $this->_request->getPost('costTitle');

                 $costAmount = $this->_request->getPost('costAmount');

                $costs  = new Application_Model_DbTable_Costs();

                if($costs->addCosts($costTitle, $costAmount))
                {
                    echo "suces";
                }

               // $this->_helper->redirector('index');
            }
            else
            {
                $form->populate($formData);  
            }
        }
    }
}

And the jQuery that is passing data is as follows:

  $('#cost').submit(function (){
      data = {
      "cost_title":"cost_title",
      "cost_amount":"cost_amount"
  };

  $.ajax({
            dataType: 'json',
            url: '/index/add',
            type: 'POST',
            data: data,

            success: function (response) {
             alert(response);
            },

            timeout: 13*60*1000,
            error: function(){
                alert("error!");
            }
        });
  });

I am getting always error.

What is the problem in this code?

Thanks in advance.

4
  • Inspect the ajax request in firebug... Is the response valid JSON? jsonlint.com Commented Jun 10, 2011 at 11:58
  • Getting the whole js as response! Commented Jun 10, 2011 at 12:13
  • 1
    Can you paste the error that you get? Commented Jun 10, 2011 at 12:18
  • the data insertion is successful it supposed to be in success: but going to error: .where I am getting error alert set by me,(look at my js code) Commented Jun 10, 2011 at 12:20

2 Answers 2

3

I would strongly recommend you implement the newest Zend/AJAX methods.

// Inside your php controller
public function init()
{
    $ajaxContext = $this->_helper->getHelper('AjaxContext');
    $ajaxContext->addActionContext('add', 'json')
                ->initContext();
}

public function addAction()
{
// action body

$form = new Application_Form_Costs();
$form->submit->setLabel('Add');
$this->view->form = $form;

if($this->getRequest()->isPost())
{
    $formData = $this->getRequest()->getPost();
    {
        if ($form->isValid($formData))
        {
            $costTitle = $this->_request->getPost('costTitle');
            $costAmount = $this->_request->getPost('costAmount');
            $costs  = new Application_Model_DbTable_Costs();
            if($costs->addCosts($costTitle, $costAmount))
            {
                // The view variables are returned as JSON.
                $this->view->success = "success";
            }
        }
        else
          {
          $form->populate($formData);  
        }
    }
}

// Inside your javascript file
// Assign handlers immediately after making the request,
  // and remember the jqxhr object for this request
  var jqxhr = $.get("/index/add/format/json", function(data) {
    alert(data);
  })
  .error(function() { alert("error"); });

For more information:

AjaxContext (ctrl+f)

jQuery.get()

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are getting an error on Session output. Why don't you disable the view-renderer, since you just need an answer for the request echo "suces" which is more than enough for your AJAX.

1 Comment

Since, the only problem I see here is, the chance of printing the layout/view-scripts. Did you put an exit; right after your success message. Do you have an working example somewhere?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.