Ankündigung

Einklappen
Keine Ankündigung bisher.

dynamisches Pulldown

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • dynamisches Pulldown

    Hallo zusammen!

    Eins gleich vorweg: ich hab diese Frage schon in Nebenforum gestellt, aber da wollte / konnte mir keiner antworten, also versuch ich's hier (und hoffe dass ich hier auch eine Lösung auf mein Problem finde).

    Ich möchte ein dynamisches Pulldown für mein Formular machen.
    Man wählt zuerst den Monat, dann wird (via javascript) festgelegt, wieviele Tage zur Verfügung stehen.

    zB 01 (Jänner) = 31, 02 (Februar) = 28

    Mein Code ist folgender:
    PHP-Code:
    <?
    // Pulldown - Monate

    $monat = "12";

    ?>
    <form name="geburtstag" action="xxx.php" >
    <?
    echo "MONATE ";
    echo "<select name='month_a' onchange='dynam_tage()'>";
    for($i = 1; $i <= $monat; $i++)
    {
       $tagesliste .= sprintf("<option value=\"%02d\">%02d</option>", $i, $i);
       printf("<option value=\"%02d\">%02d</option>", $i, $i);
    }
    echo "</select>";

    // dynamisches Pulldown - Tage

    echo "dynamische TAGE ";
    echo "<select>";
    for($i = 1; $i <= $monat['month_a']; $i++)
    {
       $tagesliste .= sprintf("<option value=\"%02d\">%02d</option>", $i, $i);
       printf("<option value=\"%02d\">%02d</option>", $i, $i);
    }
    echo "</select>";

    echo "


    ";
    echo $mon_array['01'];
    echo "


    ";
    ?>
    </form>
    <?
    ?>
    die javascript-Funktion sieht so aus:
    Code:
    <script type="text/javascript">
       function dynam_tage()
       {
          var monat = document.geburtstag.month_a.value;
          <?= $month_a ?> = monat;
       }
    </script>
    Tja, so wie ich's derzeit versuche geht's schonmal nicht...
    Ich kann nämlich keine php-Variablen mit javascript füllen...

    --> Ich brauche also eine javascriptfunktion die das ganze dynamisch macht
    --> weiß jemand wie die aussehen muss?

    Vielen Dank im Vorraus für jede Antwort!

  • #2
    versuchs mal damit:
    Code:
    <html>
      <head>
        <script language="JavaScript">
          function get_days(opts_m, opts_d) {
            for (i = 0; i < 12; i++)
              if (opts_m[i].selected) {
                if (i == 1)
                  var nod = 28;
                else if ((i < 6 && i % 2) || (i > 7 && i % 2 == 0))
                  var nod = 30;
                else
                  var nod = 31;  
                opts_d.length = nod;
                for (id = 0; id < nod; id++) {
                  opts_d[id].text = id + 1;
                  opts_d[id].value = id + 1;
                }
                break;
              }
          }
        </script>
      </head>
    
      <body onLoad="get_days(document.forms[0].elements[0].options, document.forms[0].elements[1].options)">
        <form>
          <select onChange="get_days(document.forms[0].elements[0].options, document.forms[0].elements[1].options)">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
          </select>
          <select>
            
          </select>
        </form>
      </body>
    </html>
    zweites select-element bitte nicht entfernen!

    /edit: das erste selectfeld kannst du natürlich weiterhin mit php erzeugen!

    Kommentar


    • #3
      Hm, klingt ganz gut, danke!

      Hab's aber gestern Abend dann noch alleine geschafft!

      Wie finded ihr meine Lösung:
      Code:
      <script type="text/javascript">
      	function dynam_tage_A()
      	{
      		var array_monate = new Array;
      		array_monate["01"] = 31;	array_monate["02"] = 28;	array_monate["03"] = 31;
      		array_monate["04"] = 30;	array_monate["05"] = 31;	array_monate["06"] = 30;
      		array_monate["07"] = 31;	array_monate["08"] = 31;	array_monate["09"] = 30;
      		array_monate["10"] = 31;	array_monate["11"] = 30;	array_monate["12"] = 31;
      
      		var aktuell = document.geburtstag.month_a.value;
      		aktuell = array_monate[aktuell];
      		var i = 0;
      		var tagesliste = 0;
      		for(i = 0; i <= aktuell; i++)
      		{
      			if(i+1 < 10)
      			{
      				tagesliste = new Option("0"+(i+1));
      			}
      			else
      			{
      				tagesliste = new Option(i+1);
      			}
      			document.geburtstag.day_a.options[i] = tagesliste;
      		}
      		document.geburtstag.day_a.options.length = aktuell;
      	}
      
      	function dynam_tage_E()
      	{
      		var array_monate = new Array;
      		array_monate["01"] = 31;	array_monate["02"] = 28;	array_monate["03"] = 31;
      		array_monate["04"] = 30;	array_monate["05"] = 31;	array_monate["06"] = 30;
      		array_monate["07"] = 31;	array_monate["08"] = 31;	array_monate["09"] = 30;
      		array_monate["10"] = 31;	array_monate["11"] = 30;	array_monate["12"] = 31;
      
      		var aktuell = document.geburtstag.month_e.value;
      		aktuell = array_monate[aktuell];
      		var i = 0;
      		var tagesliste = 0;
      		for(i = 0; i <= aktuell; i++)
      		{
      			if(i+1 < 10)
      			{
      				tagesliste = new Option("0"+(i+1));
      			}
      			else
      			{
      				tagesliste = new Option(i+1);
      			}
      			document.geburtstag.day_e.options[i] = tagesliste;
      		}
      		document.geburtstag.day_e.options.length = aktuell;
      	}
      </script>

      Kommentar

      Lädt...
      X