Output form fields values in submit confirm dialog

Hello everyone,

I’ve searched a lot on this topic but I couldn’t find anything useful, I hope someone can help me here.

I have a form in my application that submits data using a standard CHtml submitButton. I need to ask confirmation from the user before submitting, and in particular the user must be careful about a couple of values in the form, so I wanted to include them in the confirmation message.

Some sample code:


 <div class="row">

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

        <?php echo $form->textField($model, 'taxA'); ?>

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

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

        <?php echo $form->textField($model, 'taxB'); ?>

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

    </div>

<?php $htmlOptions = array('id' => 'submitbutton',

          'confirm'=>'Are you sure you want to save data with taxA X% and taxB Y%?');

echo CHtml::submitButton('Save the data',$htmlOptions); ?>

I would want the confirm message to have the values from the fields instead of X and Y. So if the user has written 4 and 25 for instance, it would display "Are you sure you want to save data with taxA 4% and taxB 25%?"

I thought there’s probably some way to add them with JavaScript but I’m not really sure how to. Any idea?

Thanks in advance

I’ll solve this using jquery.





$this->clientScript->registerScript('js_check', <<< EOT_JS_CHECK


     $('#submitbutton').on('click', function(ev) {

 

            // if taxA id field is #taxA, and taxB id field is #tabX

            var taxA = $('#taxA').val();

            var taxB = $('#taxB').val();


            if(

              false == confirm('Are you sure you want to save data with taxA '+taxA+'% and taxB '+taxB+'%?')

            )

            {

                ev.preventDefault();

            }

     });


EOT_JS_CHECK

);


 <div class="row">

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

        <?php echo $form->textField($model, 'taxA'); ?>

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

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

        <?php echo $form->textField($model, 'taxB'); ?>

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

    </div>

<?php $htmlOptions = array('id' => 'submitbutton');

echo CHtml::submitButton('Save the data',$htmlOptions); ?>




Perfect! Thank you! :D