如何使用php在同一页面中显示结果?

时间:2015-11-01 10:34:48

标签: php forms

我需要创建一个php页面,允许用户从select中选择一个选项。 select的选项是用PHP生成的。当用户选择选项时,我必须在同一页面中显示处理结果。对于istance:

<h3 align="center">SELECT AN EXTENSION</h3>
    <form class="form-group" method="POST" action=leggiFile.php>
        <select class="form-control" name="select[]" id="estensioni">
            <option id="tutti">Tutti i file</option>

                <?php
                    if(isset($_POST['submit']))
                    {
                        $estensione = implode($_POST['select']);
                        $elencoFile = array();
                        $cartella = 'grafici';
                        $elencoFile = glob($cartella.'/'.'*'.'.'.$estensione);
                        echo "<textarea class=\"form-control\" id=\"risultato\" rows=\"10\" disabled>
                            </textarea>";
                        foreach($elencoFile as $file)
                        {
                            echo (basename($file));
                        }
                        exit();
                    }
                    else
                    {
                        generaSelect();
                    }

                      function estraiEstensione($filename) 
                      {
                        $ext = explode(".", $filename);
                        return $ext[count($ext)-1];  
                      }
                      function generaSelect()
                      {


                      $elencoFile = array(); 



                      $cartella = 'grafici';

                      if($handle = opendir($cartella))
                      {


                         while (($file = readdir($handle))!==false)
                         {

                            if ($file != "." && $file != "..")
                            {

                                 $elencoFile[] = estraiEstensione($file);
                            } 
                         }

                         closedir($handle);



                         $arrayEstensioni = array();
                         for($i=0;$i<count($elencoFile)-1;$i++)
                         {
                            for($j=$i+1;$j<count($elencoFile);$j++)
                            {
                                if(strcmp($elencoFile[$i],$elencoFile[$j])==0)
                                {
                                    $elencoFile[$i]="0";
                                }
                            }
                         }

                        $j=0;
                        //Carciamento del vettore "vet2"
                        for($i=0;$i<count($elencoFile);$i++)
                        {


                            if(strcmp($elencoFile[$i],"0")!=0)
                            {
                                $arrayEstensioni[$j]=$elencoFile[$i];
                                $j++;
                            }
                        }


                         for($i=0;$i<count($arrayEstensioni);$i++)
                          {
                              echo ("<option value=\"$arrayEstensioni[$i]\">$arrayEstensioni[$i]</option>"."\n");

                          }
                          //Generazione dei tag di chiusura.
                      }
                      else
                      {

                      echo ("Cartella non trovata");
                      }
                    }




                    ?>

    </select>
    <input type="submit"/>
    </form>
    <form class="form-group" id="res">
        <textarea class="form-control" id="risultato" rows="10" disabled>
        <!--I have to show the extensions in this text area-->
        </textarea>
    </form>

0 个答案:

没有答案