Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony 1.4: How I can retrieve the selected value with AJAX's function in select dependent?
    primarykey
    data
    text
    <p>In my database I have two related fields. The second field depends on the value selected in the first. The relations are:</p> <p><img src="https://i.stack.imgur.com/UBeLe.jpg" alt="enter image description here"></p> <p>The function I use in the form of table "conflictos_1" is:</p> <pre><code>&lt;!--Aquí el javascript para select dependientes--&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $("#conflictos1_id_sector_actividad").change(function() { var id_sub = $(this).val(); if(id_sub != '') { $.ajax ({ type: "POST", url: '&lt;?php echo url_for('conflictos/subsector'); ?&gt;'+ '?id=' + id_sub, cache: false, data: "id_sub="+ id_sub, success: function(data) { $("#conflictos1_id_subsector_actividad").html(data); // but it does not select the value of dropdown list. } }); } else { $("#conflictos1_id_subsector_actividad").html("&lt;option value=''&gt;-- No se ha seleccionado subsector --&lt;/option&gt;"); } return false; }); }); &lt;/script&gt; </code></pre> <p>When I add a new record, everything works fine. But when I edit a record, the select dependent, does not show "selected" value. In edit mode, when I look at the field "id_subsector_actividad", the selected value should be, for example, <code>&lt;option value="37 " selected="selected"&gt;</code>: This is what I see on my form when I inspect the element created with AJAX's function:</p> <pre><code>&lt;select name="conflictos1[id_subsector_actividad]" id="conflictos1_id_subsector_actividad"&gt; &lt;option value="29 "&gt;14.1 Meretrices&lt;/option&gt; &lt;option value="30 "&gt;Preparación de alimentos y comedor&lt;/option&gt; &lt;option value="31 "&gt;Seguridad&lt;/option&gt; &lt;option value="37 "&gt;redes sanitarias&lt;/option&gt; &lt;/select&gt; </code></pre> <p>This is what I WANT to see:</p> <pre><code>&lt;select name="conflictos1[id_subsector_actividad]" id="conflictos1_id_subsector_actividad"&gt; &lt;option value="29 "&gt;14.1 Meretrices&lt;/option&gt; &lt;option value="30 "&gt;Preparación de alimentos y comedor&lt;/option&gt; &lt;option value="31 "&gt;Seguridad&lt;/option&gt; &lt;option value="37 " selected="selected"&gt;redes sanitarias&lt;/option&gt; &lt;/select&gt; </code></pre> <p>I use this function to filter records in the table "Subsector_actividad_ta8"(I work with Symfony 1.4 and Doctrine): </p> <pre><code> public function executeSubsector() { $id_sub = $_POST['id_sub']; $this-&gt;subsec= Doctrine_Core::getTable('SubsectorActividadTa8') -&gt;createQuery('a') -&gt;where('a.id_sector = ?', $id_sub) -&gt;execute(); } </code></pre> <p>My question is: What should I change in the AJAX's function, to display the "selected" value in the second field, when I am editing an existing record?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload