Page 1 of 1

Zend/jQuery interaction

Posted: Wed Nov 04, 2009 7:58 pm
by Kaz
So, I'm building a site for work, and I have a form with a submit button. The input fields of the form correspond to fields in the database, and I'd like for the button to make an AJAX request using jQuery to save the data, then do nothing. However it goes ahead and renders a blank page with whatever I echo...

Here is relevant code:

Code: Select all

In IndexController:

    public function init()
    {
         //other stuff
	$ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('save', 'html')
                          ->initContext();
    }

    public function saveAction()
    {
        //Shouldn't need to do this?
    	$this->_helper->layout->disableLayout();
     	$this->_helper->viewRenderer->setNoRender(true);

	$fname = $this->getRequest()->getParam('fname');
	$this->user->setFname($fname);
	$this->user->save();

	$response = new Zend_Controller_Response_Http();
	$this->setResponse($response);
     	
     	echo 'fcku';
    }

Code: Select all

Javascript:
function saveData(event)
{
	var fname = $('#fname').value();
	
	$.post("./index/save", "fname="+fname, alert("fucku"));
	
	event.preventDefault();
	return false;
}
Is this the proper way of doing it? What am I missing? Thanks :)

Re: Zend/jQuery interaction

Posted: Thu Nov 19, 2009 6:44 pm
by bitWISE
Try using this, rather than post:
http://docs.jquery.com/Ajax/jQuery.ajax

Code: Select all

$.ajax({
   type: "POST",
   url: "./index/save",
   data: "fname=" + fname,
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });

Re: Zend/jQuery interaction

Posted: Thu Nov 19, 2009 7:05 pm
by ^misantropia^
I somehow missed this one but anyway, here goes. Replace this:

Code: Select all

$.post("./index/save", "fname="+fname, alert("fucku"));
With this:

Code: Select all

$.post('save', $('#myform').serialize(), function(response) { alert(response); });
Dinner time now, but I'll explain why later tonight.