Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    (SOLUCIONADO) Como poner valor por defecto en un Form_Select

    Scheduled Pinned Locked Moved Español
    3 Posts 1 Posters 1.1k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      javcasta
      last edited by

      Hola

      Ando implementado una GUI para https://forum.pfsense.org/index.php?topic=112715.msg628966#msg628966

      Por ahora, mi primera duda es:

      En un formulario:

      /*
      Javier Castañon - 2016 - https://javcasta.com
      mywpad.php
      */
      require("guiconfig.inc");
      require_once("config.inc");
      require_once("globals.inc");
      require_once("pfsense-utils.inc");
      require_once("functions.inc");
      require_once("/usr/local/www/widgets/include/interfaces.inc");
      global $config, $g;
      $config = parse_config(true);
      
      $lasips = array();
      $lasinterfaces = array();
      $lasintsys = array();
      $lasmasks = array();
      $ifdescrs = get_configured_interface_with_descr();
      $i = 0;
      foreach ($ifdescrs as $ifdescr => $ifname) {
      $ifinfo = get_interface_info($ifdescr);
      //print_r($ifinfo);
      if ($ifinfo['status'] == "up" || $ifinfo['status'] == "associated") {
        $i++;
         if (strpos($ifname, "LAN") !== false) {
           $choose = $i; 
           //echo $i . " " . "\033[32m".$ifname." - ".$ifinfo['ipaddr']." \033[0m \n";     
           $lasips[] = $ifinfo['ipaddr'];
           $lasmasks[] = $ifinfo['subnet'];
           $lasinterfaces[] = $ifname;
           $lasintsys[] = array_search ("$ifname", $ifdescrs);
         }
         else { 
           //echo $i ." " . $ifname . ' ' . $ifinfo['ipaddr'] . " \033[0m \n";
           $lasips[] = $ifinfo['ipaddr'];
           $lasmasks[] = $ifinfo['subnet'];
           $lasinterfaces[] = $ifname;
           $lasintsys[] = array_search ("$ifname", $ifdescrs);
           }
      	}
      } //end foreach
      
      /*
      if (!is_array($config['mywpad']['config']))
      	$config['mywpad']['config'] = array();
      
      $a_mywpad = &$config['mywpad']['config'];
      */
      
      $a_mywpad = Array();
      
      include("head.inc");
      
      $form = new Form;
      $section = new Form_Section('My WPAD Config.');
      
      /*
      array(
      		'0' => 'LAN',
      		'1' => 'WAN',
      		'2' => 'OPT1'
      	)
      */
      $section->addInput(new Form_Select(
      	'interface',
      	'Interface',
      	'LAN',      // Initial value. If it matches on of the selectors it will be highlighted
      	$lasinterfaces
      ))->setHelp('Usually LAN interface' . '
      ' .
                  '---');
      
      $section->addInput(new Form_Input(
      	'ip',
      	'IP',
      	'text',
      	$lasips[$choose - 1]
      ))->setHelp("IP where nginx is listen proxy.pac / wpad.dat /wpad.da queries");
      
      $section->addInput(new Form_Input(
      	'port',
      	'Port',
      	'text',
      	80
      ))->setHelp("Port : Usually port tcp 80");
      
      $section->addInput(new Form_Textarea(
      	'proxypac',
      	'proxy.pac',
      	$pconfig['proxypac']
      ))->setHelp("The **proxy.pac** file.");
      
      $form->add($section);
      
      $btncncl = new Form_Button(
          'cancel',
          'Cancel'
      );
      
      $btncncl->removeClass('btn-primary')->addClass('btn-danger');
      
      $form->addGlobal($btncncl);
      
      print $form;
      ?>
      
      

      Al añadir un "Form_Select", ¿como hago para que autoseleccione el valor por defecto de la interfaz LAN??

      $section->addInput(new Form_Select(
      'interface',
      'Interface',
      'LAN',      // Initial value. If it matches on of the selectors it will be highlighted
      $lasinterfaces
      ))->setHelp('Usually LAN interface' . '
      ' .
                  '–-');

      Salu2

      Javier Castañón
      Técnico de comunicaciones, soporte y sistemas.

      Mi web: https://javcasta.com/

      Soporte scripting/pfSense https://javcasta.com/soporte/

      1 Reply Last reply Reply Quote 0
      • J
        javcasta
        last edited by

        Hola.

        Tras hacer pruebas, ya está solucionado:

        Simplemente poniendo el índice (valor númerico) del array

        $section->addInput(new Form_Select(
        	'interface',
        	'Interface',
        	$choose - 1,      // Initial value. If it matches on of the selectors it will be highlighted
        	$lasinterfaces
        ))->setHelp('Usually LAN interface');
        

        Y el Form_Select autoselecciona el índice $choose-1 del array $lasinterfaces

        Salu2

        Javier Castañón
        Técnico de comunicaciones, soporte y sistemas.

        Mi web: https://javcasta.com/

        Soporte scripting/pfSense https://javcasta.com/soporte/

        1 Reply Last reply Reply Quote 0
        • J
          javcasta
          last edited by

          Hola.

          Para no abrir otro hilo, sigo en este con mis dudas y experiencia en la GUI para el script WPADcliSetup.php.

          Ahora mi problema era como actualizar los datos del formulario al cambiar el valor del Form_Select.

          Lo solucioné así:

          //...
          if (isset($_GET['elchoose'])) { $choose = $_GET['elchoose']; }
          
          $section->addInput(new Form_Select(
          	'interface',
          	'Interface',
          	$choose - 1,      // Initial value. If it matches on of the selectors it will be highlighted
          	$lasinterfaces
          ))->setHelp('Usually LAN interface');
          //...
          //al final
          print $form;
          
          //mywpad.php?choose=1
          ?>
          
          

          Con javascript defino un "Listener" para que "escuche" el evento "change" del Form_Select (obtenido por su Id), y al cambiar en el Form_Select la interfaz, los datos relacionados en el formulario cambian y se corresponden.

          Y funciona :)

          Salu2


          –-

          Javier Castañón
          Técnico de comunicaciones, soporte y sistemas.

          Mi web: https://javcasta.com/

          Soporte scripting/pfSense https://javcasta.com/soporte/

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.