简体   繁体   中英

Database, Jquery, Ajax and Zend Framework. How to use all togheter?

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

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 :)

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

如果你想使用jQuery来显示这条消息,你可以使用appendTo()函数将text / html注入到另一个html元素中,如下所示:

$('My Message').appendTo('#divStatus');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM