Jump to content

Recommended Posts

I am trying to update the second select box 'subcategory_id'  but I don't know how to return the query values back to the control/twig template. All of the examples I have seen echo back html from a option while loop.

 

Should I be echo back the html as per other common examples or is there a more twig compliant way to do this, maybe returning an array variable.

 

Thanks in advance, James

catalogues.php
  
<script>
$('#category_id').change( function() {
    checkCategory();   // checks value and enables/disables subcategory control 
    var catid = $(this).val();
    var dataString = 'catid='+catid;
    $.ajax ({
    type: "POST",
    url: "catalogues.php",
    data: dataString,
    cache: false,
    success: function() {
       $("#subcategory_id")..........;   // ?????
      }
    }) 
  }); 

</script>

<?php

if(isset($_POST['catid'] )) {

  $catid=$_POST['catid'];
  $sql_subcategory = "SELECT
                  catalogues_categories.id,
                  catalogues_categories.category,
                  catalogues_categories.parent_id,
                  catalogues_categories.inuse
                  FROM
                  catalogues_categories
                  WHERE
                  catalogues_categories.parent_id = {$catid} AND
                  catalogues_categories.inuse = 1
                  ORDER BY
                  catalogues_categories.category ASC";

  $query_subcategory = $conn->query($sql_subcategory);
  $results_subcategory = $query_subcategory->fetchAll();
} else {
  $results_subcategory = null;
  }

?>

The result you return is passed to the success function.

 

success: function(data) {
  var el = document.getElementById('subcategory_id');
  
  $(el).empty(); // remove all HTML inside <select/>
  $.each(data, function(i, option) { // add each <option/> from data
    $('<option value="' + option.id + '">' + option.name + '</option>').appendTo(el);
  });
}
Edited by ignace

Thanks but this returns a error in the js console (chrome) and the select control is empty.

Uncaught TypeError: Cannot use 'in' operator to search for '3951' in <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"> </script>
Edited by 4xjbh

Should line 32 be:     ' + option.category + ' instead of ' + option.name + ' 

$('#category_id').change( function() {
    checkCategory();   // checks value and enables/disables subcategory control 
    var catid = $(this).val();
    var dataString = 'catid='+catid;
    $.ajax ({
      type: "POST",
      url: "catalogues.php",
      data: dataString,
      cache: false,
      success: function(data) {
        var el = document.getElementById('subcategory_id');      
        $(el).empty(); // remove all HTML inside <select/>
        $.each(data, function(i, option) { // add each <option/> from data
          $('<option value="' + option.id + '">' + option.name + '</option>').appendTo(el);
        });
      } 
    });
  });
Edited by 4xjbh
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.