Zend/jQuery interaction

Locked
Kaz
Posts: 1077
Joined: Wed Mar 08, 2006 3:43 am

Zend/jQuery interaction

Post 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 :)
bitWISE
Posts: 10704
Joined: Wed Dec 08, 1999 8:00 am

Re: Zend/jQuery interaction

Post 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 );
   }
 });
^misantropia^
Posts: 4022
Joined: Sat Mar 12, 2005 6:24 pm

Re: Zend/jQuery interaction

Post 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.
Locked