Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

Could you point how to obtain a variable defined from inside a bootstrap modal screen with a php code running inside ?

Hi Experts

Could you point how to obtain a variable defined from inside a bootstrap modal screen with a php code running inside ?

I have a modal screen with a php grid opened inside it, accordingly to:
User generated image
The code that generates it:
            <!-- Button trigger modal -->
            <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
                C
            </button>

            <!-- Modal -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog" role="document">
                <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                 <h4 class="modal-title" id="myModalLabel">Escolha o Centro de Responsabilidade</h4>
                </div>
                 <div class="modal-body">

                     <?php

                          $KoolControlsFolder = "../KoolPHPSuite/KoolControls"; 

	                   require $KoolControlsFolder . "/KoolAjax/koolajax.php";
	                   $koolajax->scriptFolder = $KoolControlsFolder . "/KoolAjax";

	                   require $KoolControlsFolder . "/KoolGrid/koolgrid.php";
	                   require $KoolControlsFolder . "/KoolCalendar/koolcalendar.php";

	                   date_default_timezone_set('America/Sao_Paulo');

	                   echo ("<br/>");

	                   $db_con = mysql_connect($db_host,$db_user,$db_word);

	                   $ds = new MySQLDataSource($db_con); 

                       // CR(s) lançáveis 
	                   $ds->SelectCommand =
		                  "SELECT SB_CR, DESCRICAO  from cr  WHERE NIVEL=1";

	                   $grid = new KoolGrid("grid");
	                   $grid->scriptFolder = $KoolControlsFolder . "/KoolGrid";
	                   $grid->styleFolder = "default";
                          $grid->Localization->Load($KoolControlsFolder."/KoolGrid/localization/vn.xml");

                       $grid->AllowFiltering = true;
	                   $grid->AjaxEnabled = true;
	                   $grid->DataSource = $ds;
	                   $grid->MasterTable->Pager = new GridPrevNextAndNumericPager();
	                   $grid->ColumnWrap = true;
	                   $grid->AllowEditing = true;
	                   $grid->AllowDeleting = true;
	                   $grid->MasterTable->DataKeyNames = "SB_CR"; // Need to set to get selection

	                   $grid->Width = "500px";
	                   $grid->ShowStatus = false;
	                   $grid->PageSize = 10;

	                   $grid->MasterTable->InsertSettings->ColumnNumber = 1;
	                   $grid->AllowSelecting = true; // Allow row selecting

	                   $column = new GridBoundColumn();
	                   $column->HeaderText = "CR";
	                   $column->DataField = "SB_CR";
	                   $column->Width = "100px";
	                   $grid->MasterTable->AddColumn($column);

	                   $column = new GridBoundColumn();
	                   $column->HeaderText = "Descrição";
	                   $column->DataField = "DESCRICAO";
	                   $column->Width = "400px";
	                   $grid->MasterTable->AddColumn($column);
                            
	                   $grid->Process();

	                   //Get selected keys after grid processing
	                   $selected_keys = $grid->GetInstanceMasterTable()->SelectedKeys;
    
                       echo $koolajax->Render();
	                   echo $grid->Render();
                   ?>
                
                </div>
                <div class="modal-footer">
                           <button type="button" class="btn btn-default" data-dismiss="modal">Sair</button>
                           <button type="button" class="btn btn-primary">OK</button>
                </div>
                
            </div>
            </div>
            </div>

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

Open in new window


I know that the grid's selected  value, detained by the php code inside the modal screen is:

$selected_keys[0]["SB_CR"]

How to return it to the form that called the modal screen after click OK button?

Thanks in advance.
img_ee_007_070516.png
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eduardo Fuerte

ASKER

Hi

Could you give a look on what I adapted accordingly what you posted and maybe suggest what is needed?

User generated image

          <div class="form-group">
            <div class="row colbox">
            <div class="col-lg-4 col-sm-4">
               <label for="cr" class="control-label">CR</label>
            </div>
            <div class="col-lg-6 col-sm-6">
                <?php
                $attributes = 'class = "form-control" id = "cr"';
                echo form_dropdown('cr',$cr,set_value('cr'),$attributes);?>
                <span class="text-danger"><?php echo form_error('cr'); ?></span>
            </div>
            
            <div class="col-lg-2 col-sm-2">
            <!-- Button trigger modal -->
            <button type="button" class="btn btn-info " data-toggle="modal" data-target="#myModal">
                C
            </button>


                 <!-- The variable that must be returned  from modal-->

                   <form id="form1">
                     <input   name="id"/>
                   </form>

          
            <!-- Modal -->
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog" role="document">
                <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                 <h4 class="modal-title" id="myModalLabel">Escolha o Centro de Responsabilidade</h4>
                </div>
                 <div class="modal-body">


                     <?php

                             // Here comes php code that renderizes the grid

                      ?>

            
                <!-- Code that catches the  option choosed after the user clicks Ok, accordingly with the picture bellow -   something else must be coded here for it to work-->

                <form>
                <div>
                <button type="submit" class="btn btn-primary">OK</button>
    		      <?php
                    
        	          if (sizeof($selected_keys) > 0)
	                  {
		                 echo "Id confirmado. <b>  " . $selected_keys[0]["Id"] . "</b>";
    	                 $_SESSION['selected_keys'] = $selected_keys[0]["SB_CR"];
	                  }
                 ?>
                </div>
                </form>
                
                </div>
                <div class="modal-footer">
                       <button type="button" class="btn btn-default" data-dismiss="modal">Sair</button>
                </div>
                
            </div>
            </div>
            </div>

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


<!-- What must to be adapted here to make the user's option choice represented by:  $selected_keys[0]["SB_CR"]; to be received by the variable  "id" in the caller form  ? -->

<script type="text/javascript">
    $(function(){
        $('.coltwo').on('click', function(){ // listen for click
          var formdata =$(this).text(); //get value from td
          $('#form1 input["name=id"]').val(formdata ); // place value from formdata var to input named id
      };
    };
</script>

Open in new window


User generated image
I will look at it a little later today