+ Rispondi alla Discussione
Risultati da 1 a 1 di 1

Come realizzare delle select concatenate con PHP e jQuery #2

Ultimo Messaggio di nofcfro il:
  1. #1
    User L'avatar di nofcfro
    Data Registrazione
    May 2012
    Località
    Roma
    Messaggi
    120

    Come realizzare delle select concatenate con PHP e jQuery #2

    Similmente al precedente post anche qui si ha a che fare con chiamate ajax, php e jquery. Spero possa tornare utile a tutti!
    Ho tre tabelle "regioni" (id_reg, nome) e "province" (id_prov, id_reg, nome) e "comuni" (id_com, id_reg, id_prov, nome). Ho due menù select (regioni e provincie) collegati che prendono dati dal database, tuttavia il terzo menù (comuni) NON deve essere un select ma deve stamparmi i dati magari dentro una tabella.

    fonte: wwwPUNTOyourinspirationwebPUNTOcom/2010/09/09/come-realizzare-delle-select-concatenate-con-php-e-jquery/

    Ho sempre una pagina db_config.php e una jquery-1.3.2.js;

    un file select.php:
    Codice PHP:
    <?php
    include_once 'select.class.php';
    $opt = new SelectList();
    if(isset(
    $_POST['id_reg']))
    {
    echo 
    $opt->ShowTipi();
    die;
    }
    if(isset(
    $_POST['id_prov']))
    {
    echo 
    $opt->ShowTipi();
    die;
    }
    ?>
    E il file più importante prova.php:
    che ha tra i tags <head>
    Codice:
    <script type="text/javascript" src="jquery-1.3.2.js"></script><script type="text/javascript">
        $(document).ready(function(){
    
    
            var scegli = '<option value="0">Scegli...</option>';
            var attendere = '<option value="0">Attendere...</option>';
            
            $("select#province").html(scegli);
            $("select#province").attr("disabled", "disabled");
            $("select#comuni").html(scegli);
            $("select#comuni").attr("disabled", "disabled");
            
            
            $("select#regione").change(function(){
                var regione = $("select#regione option:selected").attr('value');
                $("select#province").html(attendere);
                $("select#province").attr("disabled", "disabled");
                $("select#comuni").html(scegli);
                $("select#comuni").attr("disabled", "disabled");
                
                $.post("select.php", {id_reg:regione}, function(data){
                    $("select#province").removeAttr("disabled"); 
                    $("select#province").html(data);    
                });
            });    
            
            $("select#province").change(function(){
                $("select#comuni").attr("disabled", "disabled");
                $("select#comuni").html(attendere);
                var provincia = $("select#province option:selected").attr('value');
                $.post("select.php", {id_prov:provincia}, function(data){
                    $("select#comuni").removeAttr("disabled");
                    $("select#comuni").html(data);    
                });
            });    
        });
        
    </script>
    Prima del tag <body>
    Codice PHP:
    <?php
    include_once 'select.class.php'$opt = new SelectList(); 
    ?>
    E nei tags <body>
    Codice HTML:
    <div id="container">
    <form action="" id="myform">
    <table width="100%" id="gabbia">
    <tr>
    <td><h2>Riepilogo:</h2>
    <table width="100%" border="0" cellpadding="0" cellspacing="0" id="a3">
    <tr>
    <td width="32%" align="center" valign="top"><strong>Seleziona una Regione:</strong><br /><br />
    <select name="regione" size="8" id="regione">
    <?php echo $opt->ShowRegione(); ?>
    </select></td>
    <td width="38%" align="center" valign="top"><strong>Seleziona Provincia:</strong><br /><br />
    <select name="province" size="8" id="province">
    <option>Scegli...</option>
    </select></td>
    <td width="30%" align="center" valign="top"><strong>Comuni:</strong><br /><br />
    <div id="comuni"></div>
    <br />
    <br /></td>
    </tr>
    </table></td></tr><tr><td><h2>&nbsp;</h2></td></table></form></p></div>
    Io ho provato a sostituire
    Codice HTML:
    <select name="comuni" size="8" id="comuni">
    <option>Scegli...</option>
    </select>
    con
    Codice HTML:
    <div id="comuni"></div>
    E nel select.class.php:
    Codice PHP:
    <?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("Impossibile connettersi al database");            mysql_select_db($db,$this->conn) OR die("Impossibile selezionare il database $db");                        return TRUE;        }                public function ShowRegioni()        {            $xxx $_SESSION['idutenti'];            $sql "SELECT * FROM regioni";            $res mysql_query($sql,$this->conn);            while($row mysql_fetch_array($res))                {                    $regione .= '<option value="' $row['id_reg'] . '">' $row['nome'] . '</option>';                }                            return $regione;        }                public function ShowProvince()        {            $sql "SELECT * FROM province WHERE id_prov=$_POST[id_reg]";            $res mysql_query($sql,$this->conn);            while($row mysql_fetch_array($res))                {                    $province .= '<option value="' $row['id_prov'] . '">' $row['nome'] . '</option>';                }                            return $province;        }                public function ShowComuni()        {            $sql "SELECT *.* FROM comuni WHERE id_com=$_POST[id_prov]";             $res mysql_query($sql,$this->conn);            while($row mysql_fetch_array($res))                {                $comuni .= '' $row['id_com'] . '' $row['nome'] . '';                }                                            return $comuni;        }}
    ?>
    Comunque nella fonte wwwPUNTOyourinspirationwebPUNTOcom/2010/09/09/come-realizzare-delle-select-concatenate-con-php-e-jquery/ trovate il download diretto.

    Come si può fare?
    Ultima modifica di nofcfro; 13-07-12 alle 19:47

+ Rispondi alla Discussione

^ Permessi di Scrittura

  • Tu non puoi inviare nuove discussioni
  • Tu non puoi inviare risposte
  • Tu non puoi inviare allegati
  • Tu non puoi modificare i tuoi messaggi
  •  
  • Il codice BB è Attivato
  • Le faccine sono Attivato
  • Il codice [IMG] è Attivato
  • Il codice [VIDEO] è Attivato
  • Il codice HTML è Disattivato
  • Trackbacks Attivato
  • Pingback Attivato
  • Refback Attivato

SEO by vBSEO 3.6.0 PL2 ©2011, Crawlability, Inc.