Yii bootstrap submit a form inside a modal window using the modal buttons

Raven_ 007 picture Raven_ 007 · Nov 25, 2013 · Viewed 16.7k times · Source

I'm trying to submit a form from inside a modal window using the modal buttons in the modal-footer. The form itself is loaded through ajax from a link (controller/action).

Just to be clearer I attached this picture: enter image description here

This is the code for the modal:

<?php $this->beginWidget('bootstrap.widgets.TbModal', array('id'=>'modal')); ?>

<div class="modal-header">
    <a class="close" data-dismiss="modal">&times;</a>
    <h4>Client Ticket Status Update</h4>
</div>
<div class="modal-body">

</div>
<div class="modal-footer">
    <?php $this->widget('bootstrap.widgets.TbButton', array(
        'buttonType'=>'ajaxSubmit',
        'type'=>'primary',
        'label'=>'Save changes',
        'url'=>'#',
        'htmlOptions'=>array('data-dismiss'=>'modal'),
    )); ?>
    <?php $this->widget('bootstrap.widgets.TbButton', array(
        'label'=>'Close',
        'url'=>'#',
        'htmlOptions'=>array('data-dismiss'=>'modal'),
    )); ?>
</div>

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

This is the javascript that loads the form inside the modal:

$("a[data-toggle=modal]").click(function(){
    var target = $(this).attr('data-target');
    var url = $(this).attr('href');
    if(url){
        $(target).find(".modal-body").load(url);
    }
});

This is the form view:

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'client-ticket-form',
    'enableAjaxValidation'=>false,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required. </p>


    <?php echo $form->errorSummary($ticket); ?>

    <div>
        <?php echo $form->labelEx($ticket,'sent_to'); ?>
        <?php echo $form->textField($ticket,'sent_to'); ?>
        <?php echo $form->error($ticket,'sent_to'); ?>
    </div>

    <div>
        <?php echo $form->labelEx($ticket,'courier'); ?>
        <?php echo $form->textField($ticket,'courier'); ?>
        <?php echo $form->error($ticket,'courier'); ?>
    </div>

    <div>
        <?php echo $form->labelEx($ticket,'awb'); ?>
        <?php echo $form->textField($ticket,'awb'); ?>
        <?php echo $form->error($ticket,'awb'); ?>
    </div>

    <div>
        <?php echo $form->labelEx($ticket,'awb_date'); ?>
        <?php echo $form->textField($ticket,'awb_date'); ?>
        <?php echo $form->error($ticket,'awb_date'); ?>
    </div>


    <div class="buttons">
        <?php echo CHtml::submitButton($ticket->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

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

</div><!-- form -->

Any help is very appreciated. If you require more information, please ask.

Answer

Thanh Kh&#225;nh picture Thanh Khánh · Nov 25, 2013

Change code at model-footer as below:

<div class="modal-footer">
<?php $this->widget('bootstrap.widgets.TbButton', array(
    'type'=>'primary',
    'label'=>'Save changes',
    'url'=>'#',
    'htmlOptions'=>array('onclick' => '$("#formID").submit()'),
)); ?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
    'label'=>'Close',
    'url'=>'#',
    'htmlOptions'=>array('data-dismiss'=>'modal'),
)); ?>