views:

229

answers:

3

I have a form rendered in the view which action is setted to the same page /index. <form action="" name="formRegistro" id="formRegistro"> and a JQuery function which is called when the form is submitted

$('#formRegistro').submit(function() {
   // Retrevies data from the form
   data = $('#someInput').val()
   //Execute AJAX
   $.get(
      'http://localhost/myproject/',
      { data },
      function(data){
        $('#divStatus').html(data);
      }
   );

  //Prevent page loading
  return false;
});

I'm using and if statement in the IndexController to change between normal view and post view if ($this->_request->isGet()) { and I'd like to output a message in the #divStatus but with I don't know how to do it

A: 

If you want to use jQuery to show this message, you can inject text/html into another html element with the appendTo() function, like this:

$('My Message').appendTo('#divStatus');
jordanstephens
+3  A: 

Ok, first off. A normal HTTP request is always a GET request, so your condition would always be true (isGet())

what do you mean you want to output a message in the #divStatus ? That seems to be what you're doing with the callback function.

Are you using Firebug? It's a Very NECESSARY tool for working with ajax requests.

Download and install it for firefox, then do the following:

  1. Press the new firebug button at the bottom of the screen
  2. reload your page
  3. try submitting the form
  4. watch the console as your ajax request gets displayed down there
  5. find out what's happening by using Firebug

I recommend using the $.ajax() function with jquery instead of the $.get() function. You'll have more control.

If you want to display a message from the ajax request in your script when it loads, you could parse the data output of the success callback function - or you could make the function return a json object.

I hope this helps. Good luck :)

arnorhs
+1  A: 

With ZF there is a very cool way to manage AJAX & "normal" requests within the same Controller class.

It is based on the fact that most JS framework send the X-Requested-With: XmlHttpRequest HTTP header.

Take a look at AjaxContext and ContextSwitch in Zend Controller Action Helpers

mexique1