Pass Variable To Javascript In Widget

I want to pass a variable to some javascript in a Bootstrap Widget. I am using bootstrap.widgets.TbButton and I want to render a modal and pass a variable to a url. The data I want is $data->id and with a normal button I would just use




<?php  echo CHtml::button('Process', array('submit'=>array('newtask/complete', 'id'=>$data->id))); ?>



However I am using the bootstrap.widgets.TbButton widget and I want to render a modal then go to a url - I am unsure how to get the ‘id’ passed into the javascript - I have




$this->widget('bootstrap.widgets.TbButton', array(

		'label'=>'Confirm Modal',

		'type'=>'warning',

		'htmlOptions'=>array(

			'onclick'=>'js:bootbox.confirm("Are you sure?",

			function(confirmed){

				if (confirmed) {

					window.location.href="index.php?r=newtask/complete&id="

				}




			})'



How can I pass the $data->id value to




window.location.href="index.php?r=newtask/complete&id="



So the formed url is e.g. .../index.php?r=newtask/complete&id=1

Hello Anthony,

Wouldn’t simple string PHP concatenation work?

I mean





$this->widget('bootstrap.widgets.TbButton', array(

                'label'=>'Confirm Modal',

                'type'=>'warning',

                'htmlOptions'=>array(

                        'onclick'=>'js:bootbox.confirm("Are you sure?",

                        function(confirmed){

                                if (confirmed) {

                                        window.location.href="index.php?r=newtask/complete&id="' . $data->id . '

                                }




                        })'



Thanks for your response - I did try this however I get in the console "Uncaught Syntax Error:Unexpected Number" however I cannot see what is causing this. It does according to the source generate the url as I want however due to the syntax error the modal will not display. According to the console it is on line 84 however that is just <div> and nothing else. The page source from the console is




<a onclick="js:bootbox.confirm(&quot;Are you sure?&quot;,

                        function(confirmed){

                                if (confirmed) {

                                        window.location.href=&quot;index.php?r=newtask/complete&amp;id=&quot;4

                                }




                        })" id="yw1" class="btn btn-warning">Confirm Import File</a>

	


</div>

Uncaught SyntaxError: Unexpected number

</div>



And the view code is




<div class="view">


	

	<b><?php echo CHtml::encode($data->getAttributeLabel('client_id')); ?>:</b>

	<?php echo CHtml::encode($data->client->business_name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('user_id')); ?>:</b>

	<?php echo CHtml::encode($data->user->real_name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('file_name')); ?>:</b>

	<?php echo CHtml::encode($data->file_name); ?>

	<br />


	<b><?php echo CHtml::encode($data->getAttributeLabel('import_date')); ?>:</b>

	<?php echo CHtml::encode(date("d-m-Y",strtotime($data->import_date))); ?>

	<br />




<?php

	$this->widget('bootstrap.widgets.TbButton', array(

                'label'=>'Confirm Import File',

                'type'=>'warning',

                'htmlOptions'=>array(

                        'onclick'=>'js:bootbox.confirm("Are you sure?",

                        function(confirmed){

                                if (confirmed) {

                                        window.location.href="index.php?r=newtask/complete&id="'.$data->id.'

                                }




                        })'

		),

	));

?>


	


</div>



If I remove $data->id the error is resolved however the url is not correctly formed. I would appreciate any help as to how to write the javascript correctly with the widget.

My bad, haven’t noticed extra quote. Try this now:





<?php

        $this->widget('bootstrap.widgets.TbButton', array(

                'label'=>'Confirm Import File',

                'type'=>'warning',

                'htmlOptions'=>array(

                        'onclick'=>'js:bootbox.confirm("Are you sure?",

                        function(confirmed){

                                if (confirmed) {

                                        window.location.href="index.php?r=newtask/complete&id='.$data->id.'";

                                }




                        })'

                ),

        ));

?>






Perfect - thank you!