user1108329
user1108329

Reputation: 23

Zend framework Jquery submit form without page refresh

been banging my head for 2 days now I am a newbie to zend but not jquery, I am trying to post a form using Zend framework but all I seem to get is the page refreshing like a normal post request. I am using Jquery 1.4.2. I have been using firebug lite for chrome to see any output but nothing. Anyone any ideas?

Cheers Scott.

Javascript

 $(function() {

    $("#saveButton").click(function () { 

    var name = $('#name').val(),
        email = $('#email').val(),      
        phone = $('#phone').val(),
        fb = $('#fb').val(),
        tw = $('#tw').val(),
        postcode = $('#postcode').val();

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: 'http://booglam.local/client/add',
        async: false,
        data: { name: name, email: email, phone: phone, fb: fb, tw: tw, postcode: postcode },
        success: function(json) {
            console.log(json.id + ' ' + json.details);
        }
    });
  });
 });

The main PHP controller is

public function addAction()
{
    $this->_helper->layout->disableLayout(); 
    $this->_helper->viewRenderer->setNoRender(TRUE);

    $data = $this->_request->getPost();
    echo Zend_Json::encode(array('name' => $data['name'], 'email' => $data['email']));
 }

Upvotes: 2

Views: 2619

Answers (1)

Phil Rykoff
Phil Rykoff

Reputation: 12087

Try return false at the end of your function in order to prevent the regular submit process to begin.

$(function() {

$("#saveButton").click(function () { 

var name = $('#name').val(),
    email = $('#email').val(),      
    phone = $('#phone').val(),
    fb = $('#fb').val(),
    tw = $('#tw').val(),
    postcode = $('#postcode').val();

$.ajax({
    type: 'POST',
    dataType: 'json',
    url: 'http://booglam.local/client/add',
    async: false,
    data: { name: name, email: email, phone: phone, fb: fb, tw: tw, postcode: postcode },
    success: function(json) {
        console.log(json.id + ' ' + json.details);
    }
});

 return false;
  });
});

Upvotes: 4

Related Questions