Re: chained select with ajax

From: Date: Mon, 18 Jul 2011 04:05:00 +0000
Subject: Re: chained select with ajax
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-314100@lists.php.net to get a copy of this message
On Jul 17, 2011, at 2:38 PM, Chris Stinemetz wrote:
This is a [Cross-post] I didn't receive any feedback from phpdb list. Hope fully there is someone out there that may offer some advice why my code isn't working correctly. Thanks all. I am trying to create a cascading seletct with 3 menu choices. For some reason my third select menu is not populating. It doesn't seem like my ajax is correctly sending $_post values to the final query in my PHP class I built. By the time I make it to the final third select menu there are no choices. Hopefully someone can find something I missed in the following code snippits. Any help is greatly appreciated. Please excuse the incorrect indentions. For some reason gmail changes it. Thanks, Chris ajax code... <script>
      $(document).ready(function(){
          $("select#type").attr("disabled","disabled");
                      $("select#store").attr("disabled","disabled");
          $("select#market").change(function(){
          $("select#type").attr("disabled","disabled");
          $("select#type").html("<option>please wait...</option>");
          var id = $("select#market option:selected").attr('value');
          $.post("select_type.php", {id:id}, function(data){
              $("select#type").removeAttr("disabled");
              $("select#type").html(data);
          });
      });
                      $("select#type").change(function(){
                      $("select#store").attr("disabled","disabled");
                      $("select#store").html("<option>please
wait...</option>");
                      var id = $("select#type option:selected").attr('value');
          $.post("select_store.php", {id:id}, function(data){
              $("select#store").removeAttr("disabled");
              $("select#store").html(data);
                      });
              });
      $("form#select_form").submit(function(){
          var market = $("select#market option:selected").attr('value');
          var type = $("select#type option:selected").attr('value');
                      var store = $("select#store
option:selected").attr('value');
          if(market>0 && type>0 && store>0)
          {
              var market = $("select#market option:selected").html();
                              var type = $("select#type
option:selected").html();
                              var store = $("select#store
option:selected").html();
              $("#result").html('your choices were: '+market +' ,
'+type +' and '+store);
          }
          else
          {
              $("#result").html("you must choose three options!");
          }
          return false;
      });
}); </script> php class for populating select menus.... <?php class SelectList {
      protected $conn;
              public function __construct()
              {
                      $this->DbConnect();
              }
              protected function DbConnect()
              {
                      include "db_config.php";
                      $this->conn =
mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
                      mysql_select_db($db,$this->conn) OR die("can
not select the database $db");
                      return TRUE;
              }
              public function ShowMarket()
              {
                      $sql = "SELECT DISTINCT id_markets FROM store_list";
                      $res = mysql_query($sql,$this->conn);
                      $market = '<option value="0">market...</option>';
                      while($row = mysql_fetch_array($res))
                      {
                              $market .= '<option value="' .
$row['id'] . '">' . $row['id_markets'] . '</option>';
                      }
                      return $market;
              }
              public function ShowType()
              {
                      $sql = "SELECT DISTINCT store_type FROM store_list";
                      $res = mysql_query($sql,$this->conn);
                      $type = '<option value="0">store type...</option>';
                      while($row = mysql_fetch_array($res))
                      {
                              $type .= '<option value="' . $row['id'] . '">' .
$row['store_type'] . '</option>';
                      }
                      return $type;
              }
              public function ShowStore()
              {
                      $sql = "SELECT store_name FROM store_list WHERE
id_markets=$_POST[id] AND store_type=$_POST[id]";
                      $res = mysql_query($sql,$this->conn);
                      $Store = '<option value="0">stores...</option>';
                      while($row = mysql_fetch_array($res))
                      {
                              $Store .= '<option value="' . $row['id'] . '">' .
$row['store_name'] . '</option>';
                      }
                      return $Store;
              }
} $opt = new SelectList(); ?> --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
It's a little hard to see what's happening here without the code for select_type.php and select_store.php... You might try throwing in some trace code sending the output to the error log (use error_log() php function to do that) or dump the output you get from the ajax returns to the client screen to see what is getting returned, if anything.

« previous php.general (#314100) next »