Usage of Ajax form - ajax

I am an average newbie to CakePHP. While reading cookbook found Ajax form and its submission. But it lacks more details.
What are the main difference between ajax form and a normal form or What are the more specific cases we need to use Ajax form over normal form?
An example will be appreciated
Thanks....

A properly implemented Ajax form is exactly the same as a regular form, except that if JavaScript is enabled a submit handler will be bound to it that will prevent the normal submission of the form and send the data using the XMLHttpRequest object. The response will then be processed by JavaScript in the current page instead of by loading an entirely new page.

This isn't necessarily a CakePHP question, it's understanding what AJAX is.
AJAX in layman's terms is basically submitting data to a website without reloading the current page. If this is the sort of feature you want, you'll need to look into the RequestHandler component and the JsHelper in the CakePHP book for the version you are using.

Related

ASP.NET MVC submit form

I know two ways of submitting form data: by having a button of type submit or by an AJAX post call. What is the difference between those two regarding performace?
This is a very general question, so here is a very general answer.
If you implement the AJAX call efficiently on the server, then you can expect AJAX to be more performant (when measured on the server) than a full page round-trip.
For instance, if you use ASP.NET, clicking a button to submit a page will cause a POST of the form data and a complete re-buildup and rendering of the page. That is not necessary with an AJAX call, if you use real AJAX and not the Microsoft AJAX Control Toolkit for ASP.NET.
Better to use Ajax.BeginForm instead of Html.BeginForm. It will prevent the full postback and will improve the performance.
Ajax POST
Ajax post prevents reload and postback.İt sends data to controller.
Form Data
İt occurs reload page and postback.İt sends data to controller.

MVC 4, Jquery Mobile, Ajax.BeginForm causing forms to submit twice

Does anyone know of any bugs or other insight to explain why when using MVC4 with jquery mobile, any form that is created using Ajax.BeginForm results in the form being submitted twice to the controller.
I had originally thought that since I am using the js bundling that the same js file might have been being included twice and might be triggering on form submit, however thats not the case, my list of js files are:
jquery-1.6.4.js
jquery-ui.1.8.11.js
jquery.mobile-1.1.0.js
jquery.unobtrusive-ajax.js
jquery.validate.js
knockout-2.0.0.js
MicrosoftAjax.js
MicrosoftMvcAjax.js
MicrosoftMvcValidation.js
Modernizr.js
Within the project Ajax is fully enabled for everything i.e. i'm not disabling anything through mobileinit.
I have not posted any form code because it literally happens with every form - a form with one field and a submit button will cause the submission twice - but only where Ajax.BeginForm is being used.
Html.BeginForm doesn't exhibit any of these problems.
I've been stuck on this for a few days now so any help would be greatly appreciated
jQuery Mobile by default hi-jacks any form submission and performs its own AJAX request. To stop this behavior you can place the data-ajax="false" attribute on any <form> tag. Also make sure to stop the regular behavior of the form so it doesn't submit normally, something like:
$('#my-form').on('submit', false);
Docs: http://jquerymobile.com/demos/1.1.0/docs/forms/forms-sample.html
You could also just use the built-in jQuery Mobile AJAX rather than including extra JS for it. Which would only require you to make the action attributes of the forms to the server-side file to which they posts.

Symfony 1.4 Ajax

I want to make a form on ajax in Symfony Framework. The problem is the only way I found is with jquery and .load function. But with this way how to pass post values? I don't want hardcode anything so be flexible, but how make ajax submission form in symfony 1.4 with post data? How pass input posts in the load function?
Thanks a lot.
.load is fine. You can grab the values of the form with jquery.
http://www.ryancoughlin.com/2009/05/04/how-to-use-jquery-to-serialize-ajax-forms/
The only issue is that you will have to bind the form manually yourself(in the controller). I consider that the bigger issue :P

is it possible to do partial postback on web?

I read some paragraphs in a book saying that it is not possible to do a partial postback for web, even AJAX is employed. Ajax will postback everything and update only ajaxfied controls.
However, on pages I made using ajax, I used Fiddler to monitor the transportation. I found when the page initial load, it loaded everything include pictures .... However, when I click a button and do a ajax postback. I can only see the some data were loaded.... Looks like it doesn't need to reload the whole page again.
I don't know if what I see is correct? Or the book I read is correct?
Thank you guys.
That depends what you put in the term "postback".
The AJAX call will send the complete form data back to the server, just as if the form was posted normally. The server will answer with a partial response that only contains the parts of the page that should be updated.
So, the request is not partial, but the response is.
I am not sure how you are posting back from the client side. I am guessing you are using UpdatePanels. How well you 'AJAX-ify' a web page depends on what method you employ.
UpdatePanels - Read Dave Ward's posting on them - http://encosia.com/2007/07/11/why-aspnet-ajax-updatepanels-are-dangerous/
PageMethods to post back to a web service, get the data and update the DOM to display the result
JQuery and other such AJAX frameworks to post back to a web service
I am sure the link above should clear things up a bit
I'm having a hard time understanding your terminology. I'm not really sure what a "postback" is, much less a "partial" one. I do know that one of the basic ways to transmit information to an HTTP server is via a POST request, which is usually used when submitting forms. If you mean to say that the entire form is transmitted when you click a submit button, I believe you'd be right.
You also seem to be doing something with AJAX, but it's difficult to tell. The whole point of AJAX is to have dynamic data displayed on a page without resorting to reloading it. Defining what to send and what to do with the results is entirely up to your own JavaScript. So unless you're using a framework, which you don't specify, there is no such thing as "ajaxified controls."
In any case, "AJAX" usually means using the XMLHttpRequest() method of modern browsers to send data to servers without refreshing the page. When you call this function, you specify exactly what data to send. This has nothing to do with HTML forms. One caveat: if you are indeed using a library for AJAX, it might impose additional limits on how you structure information to send.

Appropriate redirection of forms that use AJAX

I have many forms that use AJAX (w/ jQuery) for validation and data submission. When a form is filled out correctly, I use window.location to redirect the page after I get an acceptable response from the PHP script. On the new page, I use a session variable (set after the AJAX calls) to display the appropriate content. Please tell me if this is standard practice or please give me some suggestions.
Thanks!
Is there a reason you would use a $_SESSION variable to store the post-submission content? Standard practice would be to validate the form via AJAX but submit it in the standard way (i.e. via $_GET or $_POST) after validation. This way you don't need to store anything to a session and you'll likely have less to debug as you'll be submitting the form and displaying its results in the most widely-accepted way.
The benefit of AJAX is typically so that you can submit the form without actually having to do the redirect/refresh. You could get the same functionality by simply having your form POST to the destination URL, redirect to the appropriate place from there or send them back to the form displaying any errors that may have occurred. You could use AJAX to validate the form before the submission to save your users a redirection back to the form to fix their errors, but this is really just a convenience for them. Also, you will have to validate any user data on the server side once it has been submitted, as you can't rely on client-side validation, so you might as well forget the AJAX validation.

Resources