Question about ajaxSubmitButton

Hi everybody!

I’m having some trouble trying use an cactiveForm and an ajaxSubmitButton. The problem is, that the form gets submitted, but there is no data from the form in the ajax POST.

I have a link in my app where the form gets rendered partial, and then I show it in a jquery dialog. This works fine, but when I click the ajaxsubmit button, no values are attached to the update POST action, so it renders again the same form above the fist one, and so on.

Here is the link code which I use to show the form:




<?php

                                echo CHtml::ajaxLink(

                                    t('gui','gen.configuration'),

                                    '/company/update', 

                                    array( 

                                            'dataType' => 'html',

                                            'type' => 'POST',

                                            'success' => 'function(data) { window.showCompanyForm(data); }',

                                            'data' => array('ajax' => 1), 

                                    ),

                                    array( 

                                            'href' => '#',

                                    )

                                ); 

                            ?>

//This is the success function


<script>

function showCompanyForm(rval) {

    $('<div class="dialog hidden" id="companyForm_dialog"></div>').append(rval).dialog({

        dialogClass: 'no_title',

        resizable: false,

        modal: true,

        width: 600,

        open: function(){

            var dialog = $(this);

            $('a.close_dialog',dialog).click(function(e){

                e.preventDefault();

                dialog.dialog('close');

            });

        },

        close: function(){

            $(this).remove();

        }

                

    });

}


</script>



Here is my action code:




public function actionUpdate() {

        

        if (isset($_POST['ajax']) && $_POST['ajax'] == '1') {

           

            $model = $this->loadModel(user()->getCompanyId());


            // Uncomment the following line if AJAX validation is needed

            $this->performAjaxValidation($model);


            if (isset($_POST['Company'])) {

               

                $model->attributes = $_POST['Company'];

                if ($model->save())

                    echo 'Prueba';

                   

            } else {

               

                $this->renderPartial('_form', array(

                    'model' => $model ));

                

            }

        } else {

            throw new CHttpException(404, t('msg', 'error.404'));

           

        }

        Yii::app()->end();

    }



And finally here’s the form and submit button:





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

        'action'=>  url('company/update'),

	'id'=>'company-form',

        'htmlOptions' => array('class'=>'form'),

	'enableAjaxValidation'=>true,

        

)); ?>


<div class="dialog_content">


	<fieldset>

            <div class="grid-12-12">


	

		<?php echo $form->labelEx($model,'name'); ?>

		<?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>100)); ?>

		<?php echo $form->error($model,'name'); ?>

            </div>


            <div class="grid-12-12 field-clear">

                    <?php echo $form->labelEx($model,'short_name'); ?>

                    <?php echo $form->textField($model,'short_name',array('size'=>20,'maxlength'=>20)); ?>

                    <?php echo $form->error($model,'short_name'); ?>

            </div>

        </fieldset>

	

    </div>


    <div class="dialog_buttons">

        <?php 

            echo CHtml::ajaxSubmitButton(

              'Create',

              array('company/update'),

              array(

                  'type' => 'POST',

                'success'=>'js:'

                .'function(id){  '

                  .'alert(id);'

                  .'$(".dialog").dialog("close"); }'

               ,

              ), 

                    array('class'=>'button green'));

        ?>

        <a class="close_dialog" href="#"><span><span><?php echo t('gui','gen.close'); ?></span></span></a>

    </div>




<?php $this->endWidget(); ?>




So, it does not submits the form, cause when I check in firebug the post data, it only sends ajax : 1, and no model validation is either done…

What am I doing wrong?

Thank you very much in advance!

Cheers!

Instead of using an ajaxsubmitbutton, use a simple button and use the onsubmit event on the form.

In that case it will work even if the user will press enter on a textbox. Take a look at this wiki

Hi zaccaria, thanks for your response! I’m going to try it and I hope it works fine!