AjaxSubmitButton

Hi all,

is some time that I try but I could not reach a good solution.

I wish at the click of AjaxSubmitButton, if I have some errors, they appear errorSummary, as is the normal submit.

It is also possible with ajaxSubmitButton, get all the textfield empty, after the submit?

Well, of course is possible, but depends on your approach. ajaxSubmitButton is a wrapper for jQuery ajax, so you would do something like:




<?php

echo CHtml::ajaxSubmitButton('Save','your-url',array(

   'type'=>'POST',

   'dataType'=>'json',

   'success'=>'js:function(data){

       if(data.result==='success'){

          // do something on success, like redirect

       }else{

         $('#some-container').html(data.msg);

       }

   }',

));

?>


//the controller action that handles the ajax request

public function actionBuh(){

  

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

      $model=new Buh();

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

      if(!$model->save())

         exit(json_encode(array('result'=>'error','msg'=>CHtml::errorSummary($model))));

      else

         exit(json_encode(array('result'=>'success','msg'=>'Your data has been successfully saved')));

 

   }else{

      exit(json_encode(array('result'=>'error','msg'=>'No input data has been passed.')));

   }




}



Not tested but should work like a charm :)

Thanks for the reply, I was feeling a thing of the Genre.

Trying your code:

View:


echo CHtml::ajaxSubmitButton(

                'Salva Pagina', $this->createUrl("/Pagine/Create"), array(

                'type'=>'POST',

                "dataType" => "json",

            'beforeSend' => 'function(data){

                  $("#risposta").show();

                  $("#risposta").html("sending...");

            }',

            'success' => 'js:function(data){

               if(data.result==="success"){

                   $("#risposta").html("Salvataggio Effettuato");

                   $("#risposta").fadeOut("slow");

                   $("#container").append(data.div);                              

                }else{                  

                    $("#risposta").html(data.msg);

                   $("#risposta p").css("color","#f00");

                }

             }',

            'error' => "function(data) {

                   $('#risposta').html('<p>Errore Inaspettato</p>');

                    $('#risposta p').css('color','#f00');                  

                }",

                )

        );

Controller:


public function actionCreate() {

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

            $model = new Pagine;

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

            if (!$model->save())

                exit(json_encode(array('result' => 'error', 'msg' => CHtml::errorSummary($model))));

            else

                exit(json_encode(array('result' => 'success', 'msg' => 'Your data has been successfully saved')));

        }else {

            exit(json_encode(array('result' => 'error', 'msg' => 'No input data has been passed.')));

        }

    }

the code always ends in

No input data has-been passed.

Do not understand the error

thanks

Ah, damn it, forgot something essential, the data attribute:




<?php

echo CHtml::ajaxSubmitButton('Save','your-url',array(

   'type'=>'POST',

   'dataType'=>'json',

   'data'=>'js:$("#your-form-id").serialize()',//this one

   'success'=>'js:function(data){

       if(data.result==='success'){

          // do something on success, like redirect

       }else{

         $('#some-container').html(data.msg);

       }

   }',

));

?>



maybe i have the problem with


if(isset($_POST['Pagine']))

but my model is Pagine…

HA ?

I think i understand why you get the error, you didn’t understand what i meant with the ajax create url, so modify the create action like so:




public function actionCreate() {

        if(Yii::app()->request->isAjaxRequest){


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

            $model = new Pagine;

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

            if (!$model->save())

                exit(json_encode(array('result' => 'error', 'msg' => CHtml::errorSummary($model))));

            else

                exit(json_encode(array('result' => 'success', 'msg' => 'Your data has been successfully saved')));

        }else {

            exit(json_encode(array('result' => 'error', 'msg' => 'No input data has been passed.')));

        }


        }


     // render the create page[this is an example, do it to match your case]

     $model=new Pagine();

     $this->render('create',array('model'=>$model));//this view will have the form in it

    }



thanks again

i have this error:

Undefined index: Pagine

because i use

if (isset($_POST[‘Pagine’])) {

Pagine is my Model, and create is my Action.

I Must use $_POST[‘myModel’] right?

I joined my code and your code and this is the result:

View:


 echo CHtml::ajaxSubmitButton(

                'Salva Pagina', $this->createUrl("/NibiruCms/Pagine/Create"), array(

                'type'=>'POST',

                "dataType" => "json",

                //'data'=>'$("#crea_pagina").serialize()',//with this, i have always error (text-field blank of example)


            'beforeSend' => 'function(data){

                  $("#risposta").show();

                  $("#risposta").html("sending...");

            }',

            'success' => 'function(data){

               if(data.result==="error"){

                   $("#risposta").html(data.msg);

                   $("#risposta p").css("color","#f00");

                }else{

                   $("#risposta").html("Salvataggio Effettuato");

                   $("#risposta").fadeOut("slow");

                   $("#container").append(data.div);

                }

             }',

            'error' => "function(data) {

                   $('#risposta').html('<p>Errore Inaspettato</p>');

                    $('#risposta p').css('color','#f00');

                }",

                )

        );

Controller:


public function actionCreate() {

        $model = new Pagine;

        $errors = CActiveForm::validate($model);

        if ($errors !== '[]') {

            exit(json_encode(array('result' => 'error', 'msg' => CHtml::errorSummary($model))));

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

        }

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

        $model->save();


        if (Yii::app()->request->isAjaxRequest) {

            echo CJSON::encode(array(

                'div' => $this->renderPartial('crea_pagina', array('box_pagina' => $model,'homepage' => $this->get_homepage()), true, false),

            ));

        }

    }

but I still doubt that I wrote above

and how to make empty fields after the success, I do a foreach in javascript?

and why you put js: before? I never use it is a mistake?

thanks very nice

nope, is not a mistake, the js: in front of a string instructs Yii to treat it as javascript.

Hi,

Can anyone tell me how to render an image in ajaxsubmitbutton. I have declared the type as image in htmloptions but it is not rendering. here is my code


<?php echo CHtml::ajaxSubmitButton('Join Us',array('users/register'),

array('update'=>'#email_error',	),

array('type'=>'image',

      'src'=>Yii::app()->request->baseUrl.'/css/images/home/joinus.jpg',

));

?>

Thanks

hi, i use

‘class’ => “my class”

and in css i use background-image. i never do like you. with class it works.