多个提交按钮的Ajax操作

时间:2017-01-29 15:33:50

标签: php ajax yii

我有三个ajaxsubmit按钮,如下所示

echo CHtml::ajaxSubmitButton(
                                        'Save', //the label
                                        CController::createUrl('healthInformation/saveData'), //the url = index if empty (or set to another controllerAction)
                                        array('update'=>'#healthData',),  // the ajax-options: display the response inside this div
                                        array('class'=>'adm-myButton','title'=>'Save','name'=>'save') //your htmlOptions
                                        );
echo CHtml::ajaxSubmitButton(
                                        'add', //the label
                                        CController::createUrl('healthInformation/saveData'),  //the url = index if empty (or set to another controllerAction)
                                        array('update'=>'#healthData',),  // the ajax-options: display the response inside this div
                                        array('class'=>'adm-myButton','title'=>'Save','name'=>'add') //your htmlOptions
                                        );
echo CHtml::ajaxSubmitButton(
                                        'delete', //the label
                                        CController::createUrl('healthInformation/saveData'),  //the url = index if empty (or set to another controllerAction)
                                        array('update'=>'#healthData',),  // the ajax-options: display the response inside this div
                                        array('class'=>'adm-myButton','title'=>'Save','name'=>'delete') //your htmlOptions
                                        );

我只能访问一个动作saveData,如下所示

public function actionSaveData()
{
   if(isset($_POST['save']))
   {
   }
  if(isset($_POST['add']))
   {
   }
 if(isset($_POST['delete']))
   {
   }
$this->renderPartial('');
}

或者必须为不同的ajaxsubmitbuttons访问不同的操作。 请任何建议表示赞赏。

1 个答案:

答案 0 :(得分:0)

一种简单的方法是使用具有不同名称的按钮

<?= Html::submitButton('Save',[ 'name'=>'save', 'value' => 'view', 'class' => 'btn btn-default',]) ?>
<?= Html::submitButton('Save&Next',[ 'name'=>'save_next', 'value' => 'print', 'class' => 'btn btn-default',]) ?>
<?= Html::submitButton('Delete',[ 'name'=>'delete', 'value' => 'print', 'class' => 'btn btn-default',]) ?>
....

然后在你的行动中

public function actionYourAction()
{
    if (isset($_POST['save')) { // or if you are using GET isset($_GET['save')
       // perform save code
       .....
       return;

   if (isset($_POST['save_next'])) {
       // perform save  & next code
       return;
    }
   if (isset($_POST['delete'])) {
       // perform delete code
       return;
    }

    ......

}