php-general Digest 18 Jul 2011 22:01:16 -0000 Issue 7405

Topics (messages 314100 through 314102):

Re: chained select with ajax
        314100 by: Tamara Temple
        314101 by: Jim Lucas

How to sum monetary variables
        314102 by: Martín Marqués

Administrivia:

To subscribe to the digest, e-mail:
        [email protected]

To unsubscribe from the digest, e-mail:
        [email protected]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin 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.


--- End Message ---
--- Begin Message ---
On 7/17/2011 11:38 AM, Chris Stinemetz wrote:
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

I have made a few assumptions in the following code... with that said

Well, without seeing more of your code, from what I can tell from what you provided, you are missing a few statements in your jQuery and you are using the wrong variables that jQuery is passing to your PHP.

Also, since I do not see you populating the #market SELECT field with jQuery, is it safe to assume that you are populating it when you create the page?

> <script>
> $(document).ready(function()
> {
>   $("select#type").attr("disabled","disabled");
>   $("select#store").attr("disabled","disabled");
>   $("select#market").change(function()
>   {

remove the following line, it is redundant

>     $("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()
>   {

remove the following line, it is redundant

>     $("select#store").attr("disabled","disabled");
>     $("select#store").html("<option>please wait...</option>");

     var m_id = $("select#market option:selected").attr('value');
     var t_id = $("select#type option:selected").attr('value');
     $.post("select_store.php",
            {market_id:m_id,type_id:t_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("cannot 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))
>     {

you are using $row['id'] below, but you do not select it above...

>       $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))
>     {

you are using $row['id'] below, but you do not select it above...

>       $type .= '<option value="' . $row['id'] . '">' .
>                $row['store_type'] . '</option>';
>     }
>     return $type;
>   }
>
>   public function ShowStore()
>   {
>     $sql = "SELECT store_name
>             FROM   store_list

Are you suppose to be using the same $_POST variable for this select statement?

>             WHERE  id_markets=$_POST[id]
>             AND    store_type=$_POST[id]";

to work with the changes that I made above in the jQuery code, you will need to change the previous two lines to the following two lines of code

>             WHERE  id_markets=$_POST['market_id']
>             AND    store_type=$_POST['type_id']";

That show fix most of your problems and get you headed down the rod to recovery...

>     $res = mysql_query($sql,$this->conn);
>     $Store = '<option value="0">stores...</option>';
>     while($row = mysql_fetch_array($res))
>     {

you are using $row['id'] below, but you do not select it above...

>     $Store .= '<option value="' . $row['id'] . '">' .
>               $row['store_name'] . '</option>';
>     }
>     return $Store;
>   }
> }
>
> $opt = new SelectList();
>
> ?>


--- End Message ---
--- Begin Message ---
I'm building a table (which is a report that has to be printed) with a
bunch of items (up to 300 in some cases) that have unitary price
(stored in a numeric(9,2) field), how many there are, and the total
price for each item. At the end of the table there is a total of all
the items.

The app is running on PHP and PostgreSQL is the backend.

The question is, how do I get the total of everything?

Running it on PHP gives one value, doing a sum() on the backend gives
another, and I'm starting to notice that even using python as a
calculator gives me errors (big ones). Right now I'm doing the maths
by hand to find out who has the biggest error, or if any is 100%
accurate.

Any ideas?

-- 
Martín Marqués
select 'martin.marques' || '@' || 'gmail.com'
DBA, Programador, Administrador

--- End Message ---

Reply via email to