Ajaxsubmitbutton Not Passing ID

Ajaxsubmitbutton Not Passing ID

本文关键字:ID Passing Not Ajaxsubmitbutton      更新时间:2023-09-26

我在使用引导模式更新数据时遇到问题。

在CGridView selectionChanged上,它将触发一个函数来显示模式对话框表单并将数据插入表单

这是我的CGridView:

<?php
    $this->widget('customCGridView', array(
                    'id'=>'contactperson-grid',
                    'itemsCssClass' => 'table table-bordered',
                    'selectionChanged'=>'showmodal',
                    'dataProvider' => $contactperson->search(),
                    'emptyText' => '',
                    'enableSorting' => false,
                    'htmlOptions' => array('class' => ' '),
                    'columns' => array('nama', 'jabatan')
    ));
?>

在selectionChanged时,它将触发此脚本打开更新对话框模式,并用选定的CGridView列数据填充表单:

function showmodal(grid_id) {     
    var keyId = $.fn.yiiGridView.getSelection(grid_id);
    keyId  = keyId[0]; //above function returns an array with single item, so get the value of the first item     
  $.ajax({
        url: '<?php echo $this->createUrl("suppliertable/personview"); ?>',
        data: {id: keyId},
        type: 'GET',
        success: function(data) {
            $("#contact-person-modal").html(data);
            $('#kontakmodalupdate').modal('show');
        }
    });
 }

以下是actionPersonView,它将在选择列时执行:

public function actionPersonView($id) {                
    $contactperson = SupplierContactPersonTable::model()->findByPk($id);
    if ($contactperson === null)
        throw new CHttpException(404, 'The requested page does not exist.');
    $this->renderPartial('updateForm', array('contactperson'=> $contactperson));              
    Yii::app()->end();
}

现在,问题来了:

当我点击更新表单上的更新按钮时,我需要将ID传递给我的操作:

public function actionPersonUpdate($id) {
    $model2=$this->loadModel2($id);                    
    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);
    if(isset($_POST['SupplierContactPersonTable']))
    {
        if($model2 === null) {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        $model2->attributes=$_POST['SupplierContactPersonTable'];
        $model2->save();
    }
}

为了传递这个值,我在表单上使用AjaxButton:

<div id="contact-person-modal">
<?php
    $form = $this->beginWidget('CActiveForm', array(
                      'id' => 'suppliercontactmodalform',
                      'htmlOptions' => array(
                                          'class' => 'smart-form',
                                          'novalidate' => '1',),
                      'enableAjaxValidation' => false,));
?>
...
<footer>
    <button type="button" class="btn btn-labeled btn-danger cancel-button" style="float: left">
        Hapus Pengguna
    </button>
    <?php
       echo CHtml::ajaxButton('Update', Yii::app()->createUrl('suppliertable/personupdate',array('id'=>$contactperson->contact_person_id)), array('type'=>'POST'), array('type'=>'submit',"class" => "btn btn-primary"));
    ?>
    <button type="button" class="btn btn-default" data-dismiss="modal">
    Batal
    </button>    
</footer>
...

然而,ID似乎没有通过。这是Firebug控制台:

POST http://localhost/webapp/index.php/suppliertable/personupdate/
400 Bad Request

正如您所看到的,没有传递任何ID。

但是,如果我使用静态参数值:

echo CHtml::ajaxButton('Update', Yii::app()->createUrl('suppliertable/personupdate',array('id'=>'5')), array('type'=>'POST'), array('type'=>'submit',"class" => "btn btn-primary"));

ID已传递:

POST http://localhost/webapp/index.php/suppliertable/personupdate/5

所以,我认为问题就在这里,不是输出值:

$contactperson->contact_person_id

这里是从Yii:生成的jQuery

jQuery('body').on('click','#yt0',function({
    jQuery.ajax({
        'type':'POST',
        'url':'/webapp/index.php/suppliertable/personupdate id=',
        'cache':false,
        'data':jQuery(this).parents("form").serialize()
    });
    return false;
});

感谢您的帮助:)

我找到了我的解决方案。

如果有人有同样的问题,我需要更新我的actionPersonView方法:

public function actionPersonView($id) {
            if (Yii::app()->request->isAjaxRequest) {
                Yii::app()->clientScript->scriptMap['jquery.js'] = false;
                Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;   
                $contactperson = SupplierContactPersonTable::model()->findByPk($id);
                if ($contactperson === null)
                    throw new CHttpException(404, 'The requested page does not exist.');
                $this->renderPartial('updateForm', array('contactperson'=> $contactperson),false,true);              
                Yii::app()->end();
            }
    }

我需要在我的actionPersonView方法中添加

$this->renderPartial('updateForm', array('contactperson'=> $contactperson),false,true);

并添加

Yii::app()->clientScript->scriptMap['jquery.js'] = false;
Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;

以防止脚本被多次执行。