Skip to content Skip to sidebar Skip to footer

Symfony 1.4: How I Can Retrieve The Selected Value With Ajax's Function In Select Dependent?

In my database I have two related fields. The second field depends on the value selected in the first. The relations are: The function I use in the form of table 'conflictos_1' is

Solution 1:

Maybe with something like:

$("option[value='37']").attr('selected', 'selected');

You have a similar question on: Set selected option of select box

Solution 2:

Here the solution I encountered: I've separated the problem in two.

Case 1: New Record

I use the same JQuery function shown above in the question. With the associated function, also shown above. This case was not part of my problem

Case 2: Edit an existing record (here was my problem)

I added for 'id_subsector_actividad' in the widget the property 'table_method'=>'Subsector' associated with the function public function Subsector() (see below code).

In the partial _form.php of conflictos1, I wrote the following code for the field id_subsector_actividad:

<?phpif (!$form->getObject()->isNew()): ?><?phpecho$form['id_subsector_actividad'] ?><?phpendif; ?><?phpif ($form->getObject()->isNew()): ?><selectname="conflictos1[id_subsector_actividad]"id="conflictos1_id_subsector_actividad"><optionvalue=""selected="selected">Seleccione sub-sector</option><?phpendif; ?>

In the action.class.php in the conflictos1, I modified the function executeEdit, with the addition of the variable $elsector:

publicfunctionexecuteEdit(sfWebRequest $request)
  {
 global$elsector;
    $this->forward404Unless($conflictos1 = Doctrine_Core::getTable('Conflictos1')->find(array($request->getParameter('id'))), sprintf('Object conflictos1 does not exist (%s).', $request->getParameter('id')));

    $elsector= $conflictos1->getIdSectorActividad(); 


    $this->form = new Conflictos1Form($conflictos1);


  }

I use $elsector in the following function created in SubsectorActividadTa8Table.class.php

publicfunctionSubsector()

    {
         global$elsector;


         if (!is_null($elsector)){
       $id_sub=$elsector;
       $query= Doctrine_Query::create()
            ->select('a.id')
            ->from('SubsectorActividadTa8 a')
         ->innerJoin('a.Conflictos1 c')

           ->where('a.id_sector = ?', $id_sub); 
               return$query->execute();  
    }
    }

Thus, Symfony displayed on the form, the value of field named IdSubsectorActividad, not previously exhibited.

In other words, now when I'm editing a record in the table conflictos1 the field named IdSubsectorActividad shows the correct value, whereas before the form does not show any value.

Now everything works fine!.

Post a Comment for "Symfony 1.4: How I Can Retrieve The Selected Value With Ajax's Function In Select Dependent?"