How to insert a Simple Dialog Box

You are viewing revision #1 of this wiki article.
This is the latest version of this article.

  1. How to Implement
  2. DialogBox.php

I build this simple PHP class (DialogBox.php at the bottom of this wiki) to help you in the dialog box usage on Yii applications. The goal is run any Yii actions into this dialog box and return the resulting value to the calling instance.

in the next image, i use this solution to open a Dialog Box that runs a sequence of 3 actions to perform a Zipcode search, when the zip code is found then the user press "Finish" button and the dialog box is automatically closed, and the selected zipcode is returned.

sample usage

How to Implement

To launch the dialog box.

this piece of code must be used to open the popup, the createDialogBox call creates a css-styled icon ready and configured to launch the jquery dialogbox.

<div id='dialogBoxLaunchIconPosition' style="margin-top: 10px;">
To start testing this dialog box sample, click this icon:
<?php DialogBox::createDialogBox(
   ,"Type your name:"

When dialog is finished then your input will be shown in this Input element:<br/>
<input id='testinput'></input>


Closing dialog and returning the desired value.

this is the code included in any action to return the dialog response value, as follows:

public function actionSample()
        $model=new SampleModel;
                // form inputs are valid, do something here
        $this->layout = "dialoglayout";


Create a file named DialogBox.php in your components folder. Copy this code into it:

   author:  Christian Salazar,
class DialogBox {
		public static function createDialogBox(
			$timer = "timer".$dialogName;
			echo "\t<div class='$iconClass' 
				onclick=\"{ $('#$dialogName').dialog('open');
				$timer(); // lanza el timer creado mas abajo
		public static function closeDialogBox($value,$actionResetTo)
			echo "<script>
				if(window.frameElement != null)

		/* crea un dialogo jquery con un IFRAME dentro
		 * el cual contendrá el action a ejecutar y que a su vez
		 * servira como PUENTE para trasmitir el resultado
		 * del dialogo mediante el atributo IFRAME.VALUE
		 * el cual será establecido por el action mediante
		 * DialoxBox::closeDialogBox.
		private static function putDialog($this_,$dialogName,$title
			$dialog = $dialogName;
			$iframeId = 'iframe'.$dialogName;
			$divForForm = "divForForm-$dialogName";
			$this_->beginWidget('zii.widgets.jui.CJuiDialog', array(
			$src = Yii::app()->baseUrl."?r=".$iframeActionSource;

			/* .iframeDialogBg y .iframeDialog  esta en iframe.css
			$iframe = "<iframe value='0' title='response' class='iframeDialog' id='$iframeId' frameborder='0' scrolling='no' src='$src'></iframe>";
			$extra="<div style='cursor:pointer;' onclick='closeDialogBox()'>close</div>";
			echo "\n\t<div class='iframeDialogBg'>\n\t\t$iframe\n\t</div>\n\t<!-- fin -->\n";
		/* es un timer que se ejecuta cuando se abre el dialogo jquery.
		 * se mantiene revisando por cambios de valor en el iframe.value
		 * este valor (iframe.value) es establecido por el script que usa
		 * el iframe, en el caso de zipcodefinder es el boton Finish. 
		 * cuando este valor pasa de null o undefined a tener un valor
		 * distinto entonces cierra el dialogo jquery, establece
		 * al INPUT con ID $idInputElement al valor devuelto.
		private static function defineTimerParaCierreDeDialogo($dialogName,$idInputElement)
			$timer = "timer".$dialogName;
			$iframe = "iframe".$dialogName;
			echo "\n";
			echo "<!-- timer de escucha de respuesta del dialogo '$dialogName'  -->";
			echo "\n";
			echo "<script>\n";
			echo "function $timer(){
					var x = document.getElementById('$iframe');
					var result = x.value;
					if(!(result == null || result == 'undefined')){
						// hay valor definido
						document.getElementById('$idInputElement').value = result;
						// cierra el dialogo
						x.value = null;