• User Newbie

    Aiuto...form a tendina marca modello prezzo

    Ciao a tutti, ho un problema con la creazione di un form a tendina.

    Ho un db mysql formato in questo modo:

    ID | Manufacturer | ModelNo | Price

    Vorrei creare un form a tendina dove scegliendo il modello (Manufacurer) appaia un'altra tendina con la lista dei modelli (ModelNo) relativa a Manufacturer, scegliendo il modello vorrei che apparisse il prezzo (Price)

    Ho provato con lo script qui sotto, opportunamente modificato, ma non riesco a farlo funzionare
    Qualcuno sa darmi una mano?
    Grazie

    <?php
    $id_regione="";
    $id_provincia="";
    $id_comune="";
    
    $id_regione=$_POST['regione'];
    $id_provincia=$_POST['provincia'];
    $id_comune=$_POST['comune'];
    ?>
    
    <form action="index.php" name="search" method="POST">
        <select onchange="document.search.submit()" name="regione">
        <option value="">Scegli regione...</option>
        <?php
        $sql_regione="SELECT nome,codice FROM regioni ORDER BY nome";
        $query_regione=@mysql_query($sql_regione) or die (mysql_error());
        while ($rigaR=mysql_fetch_array($query_regione)){
            $regione=$rigaR['nome'];
            $idRegione=$rigaR['codice'];
            echo "<option value='$idRegione'";
            if ($id_regione==$idRegione)
                echo " selected";
            echo ">".$regione ."</option>";
        }
        ?>
        </select></td>
        </tr>
        <tr>
            <td>Provincia: </td>
            <td>    
        <select onchange="document.search.submit()" name="provincia">
        <option value="">Scegli provincia...</option>
        <?php
        if(isset($id_regione)){
        $sql_provincia="SELECT * FROM province WHERE codice_regione='$id_regione' ORDER BY nome";
        $query_provincia=@mysql_query($sql_provincia) or die (mysql_error());
        while ($rigaP=mysql_fetch_array($query_provincia)){
            $provincia=$rigaP['nome'];
            $idProvincia=$rigaP['codice'];
            echo "<option value='$idProvincia'";
            if ($id_provincia==$idProvincia) {echo " selected";}
            echo ">".$provincia."</option>";
        }}
        ?>
        </select></td>
        </tr>
        <tr>
            <td>Comune: </td>
            <td>
        <select onchange="document.search.submit()" name="comune">
        <option value="">Scegli il comune...</option>
        <?php
        if(isset($id_provincia)){
        $sql_comuni="SELECT * FROM comuni WHERE codice_provincia='$id_provincia' ORDER BY nome";
        $query_comuni=@mysql_query($sql_comuni) or die (mysql_error());
        while ($rigaC=mysql_fetch_array($query_comuni)){
            $comune=$rigaC['nome'];
            $idComune=$rigaC['codice'];
            echo "<option value='$idComune'";
            if ($id_comune==$idComune) {echo " selected";}
            echo ">".$comune."</option>";
        }}
        ?>
        </select></td>
        </tr>
    </form>
    

  • User Newbie

    mi fa fare la scelta ma non riesco a far visualizzare i risultati delle scelte.
    ecco il codice che sto usando e che sembra funzionare:

    
    <?php
    
    /*session_start();*/
    
    class OptionMenu
    {
        var    $conn;
        var    $page;
        
            function OptionMenu()
            {
                $this->page = basename($_SERVER['PHP_SELF']);
                $this->DbConnectAndSelect();
                $this->ParsingAction();
                
            }
            
            function DbConnectAndSelect()
            {
                include "../OLD/conn.php";
                
                $this->conn = @mysql_connect($host,$username,$password) OR
                die("Impossibile connettersi al server");
                
                @mysql_select_db($db, $this->conn) OR
                die("Impossibile selezionare il database");
            }
            
            function JsHeader()
            {
                echo '<SCRIPT language=JavaScript>
                <!--
                function goSelect(daform)
                {
                    with(daform)
                    {
                        top.window.location=options[selectedIndex].value;
                    }
                }
                //-->
                </SCRIPT>
                ';
    
            }
            
            function ShowRegioni()
            {
                if(isset($_SESSION['Manufacturer']))
                {
                    $marca = $_SESSION['Manufacturer'];
                }
                else
                {
                    $marca = "Seleziona una marca";
                }
                
                $sql = "SELECT distinct Manufacturer FROM IT_Trade";
                $res = mysql_query($sql, $this->conn);
                
                echo '<form>
                <select onchange="goSelect(this)" size="1">
                <option>' . $marca . '</option>
                ';
                
                    while($row = mysql_fetch_array($res))
                    {
                        if($row['Manufacturer'] != $marca)
                        {
                            echo '<option value="' . $this->page . '?Manufacturer=' . $row['ID'] . '&action=ModelNo&Manufacturer=' . $row['Manufacturer'] . '">' . $row['Manufacturer'] . '</option>
                            ';
                        }
                    }
                
                echo '</select>
                </form>
                ';
            }
            
            function ShowProvince()
            {
                if(isset($_SESSION['ModelNo']))
                {
                    $modello = $_SESSION['ModelNo'];
                }
                else
                {
                    $modello = 'Seleziona modello';
                }
                
                echo '<form>
                <select onchange="goSelect(this)" size="1">
                <option>' . $modello . '</option>
                ';
                
                if($marca = $this->SelectProvincia())
                {
                    $sql = "SELECT ModelNo FROM IT_Trade WHERE Manufacturer='$marca'";
                    $res = mysql_query($sql, $this->conn);
                    
                        while($row = mysql_fetch_array($res))
                        {
                            if($row['ModelNo'] != $modello)
                            {
                                echo '<option value="' . $this->page . '?ModelNo=' . $row['ID'] . '&action=end&ModelNo=' . $row['ModelNo'] . '">' . $row['ModelNo'] . '</option>
                            ';
                            }
                        }
                }
                
                    echo '</select>
                </form>
                ';
            }
            
           function ShowComuni()
            {
                if(isset($_SESSION['Wattage']))
                {
                    $watt = $_SESSION['Wattage'];
                }
                else
                {
                    $watt = 'Seleziona un Wattage';
                }
                
                echo '<form>
                <select onchange="goSelect(this)" size="1">
                <option>' . $watt . '</option>
                ';
                
                if($_GET['ModelNo'])
                {
                    $sql = "SELECT Wattage FROM IT_Trade WHERE ModelNo='$_GET[ModelNo]'";
                    $res = mysql_query($sql, $this->conn);
                    
                        while($row = mysql_fetch_array($res))
                        {
                            echo '<option value="' . $this->page . '?Wattage=' . $row['ID'] . '&action=end&Wattage=' . $row['Wattage'] . '">' . $row['Wattage'] . '</option>
                        ';
                        }
                }
                
                    echo '</select>
                </form> 
                ';
            }
            
            function SelectProvincia()
            {
                if(!$_GET['Manufacturer'] AND !$_SESSION['Manufacturer'])
                {
                    return FALSE;
                }
                else
                {
                    if($_GET['Manufacturer'])
                    {
                        return $_GET['Manufacturer'];
                    }
                    else
                    {
                        $sql = "SELECT Manufacturer FROM IT_Trade WHERE Manufacturer='$_SESSION[Manufacturer]'";
                        $res = mysql_query($sql, $this->conn);
                        $row = mysql_fetch_array($res);
                        
                        return $row['ID'];
                    }
                }
            }
            
            function ParsingAction()
            {
                if(isset($_GET['action']))
                {
                    if($_GET['action'] == 'ModelNo')
                    {
                        $_SESSION['Manufacturer'] = $_GET['Manufacturer'];
                        unset($_SESSION['ModelNo']);
                        unset($_SESSION['Wattage']);
                    }
                    if($_GET['action'] == 'Wattage')
                    {
                        $_SESSION['ModelNo'] = $_GET['ModelNo'];
                        unset($_SESSION['Wattage']);
                    }
                    if($_GET['action'] == 'end')
                    {
                        $_SESSION['ModelNo'] = $_GET['ModelNo'];
                        $_SESSION['Wattage'] = $_GET['Wattage'];
                        echo 'Marca: ' . $marca . '<br>Modello: ' . $_SESSION['ModelNo'] . '<br>Lampada: ' . $_SESSION['Wattage'];
                        /*header("Location: result.php");
                        die;*/
                    }
                }
            }
    }
    ?>