Ajax form sumbit

I have a form with an ajaxSubmitButton, and I would like to know how I can achive, that on press "enter" the form would be submitted to the ajax request, and not the the given action?

By now, the ajax request is only fired by the ajaxSubmitButton and not by pressing the "enter" button (by pressing "enter" the form would be submitted to the given action, so I set it to "javascript: return false;").

You just need to listen onsubmit event on the form itself, instead of onclick on the button.

e.g

<form … onsubmit="runSomeJSFunction(); return false;">

</form>

Attach a .click event to the button. Something like:




<script type="text/javascript">

$(function() {

    $(".submit_button").click(function() {

        $.post(

            "../some/url,

etc.



Why not use CHtml::ajaxButton()?

I see the ajax helpers in Yii only useful for simple cases. By using jquery "manually" you get the full power of AJAX and can do much like everything you want.

Edit: Sorry, a little OT :)

And how about case when i want have ajaxValidation of CActiveForm widget and ajax submit ?

Just check validateOnSubmit.


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'town-form',

	'enableAjaxValidation'=>true,

	'clientOptions'=> array('validateOnSubmit'=>true),

)); ?>

Is that what do you need?

Yes, plus i want send already walidated form with ajax

For this you can use the clientOptions:afterValidate.

I never tested, but according to documentation it looks like that you can trigger the ajax submission here and return false, in order to stop the usual submission.

Maybe something like that:


<?php $form=$this->beginWidget('CActiveForm', array(

        'id'=>'town-form',

        'enableAjaxValidation'=>true,

        'clientOptions'=> array(

             'validateOnSubmit'=>true,

		'afterValidate'=>'js:function(form, data, hasError) {if (!hasError){ '.CHtml::ajax([...]).'; return true;}}'



    ),

)); ?>

Something like that sould work.

I have two forms and the form2 will get a id of form1 and with this id have a function that will bring and populate the form 2 with information of database … how can I do it!