• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

Variable Funktioniert nicht

SimoNxD

Mitglied
Hallo Leute hab ein kleines Problem.
Aber weiß nicht wie ich es lösen soll D:

HTML:
<form method="post" action="?s=mbo_edit">
<select style="width:140px; " class="login_input" name="team_daily">
                        <option id="1.0">2 Stunden</option>
                        <option id="1.1">2 1/2 Stunden</option>
                        <option id="1.2">3 Stunden</option>
                        <option id="1.3">3 1/2 Stunden</option>
                        <option id="1.4">4 Stunden</option>
                        <option id="1.5">6 Stunden</option>
                </select>
<input type="submit" value="Berechnen" name="rechnen">
</form>

PHP:
    if(isset($_POST['rechnen'])) {
        if($_POST['team_daily'] == '1.0') {$stunden = 2.0;}
        if($_POST['team_daily'] == '1.1') {$stunden = 2.5;}
        if($_POST['team_daily'] == '1.2') {$stunden = 3.0;}
        if($_POST['team_daily'] == '1.3') {$stunden = 3.5;}
        if($_POST['team_daily'] == '1.4') {$stunden = 4.0;}
        if($_POST['team_daily'] == '1.5') {$stunden = 6.0;}

        echo $stunden;
    }

Und dann kommt ständig die Fehlermeldung:

Code:
Notice: Undefined variable: stunden in C:\xampp\htdocs\ipanel\pages\mbo_edit.php on line 18

Woran liegt das ?

mfG
 
PHP:
<?php
$aOptions = array(
  60 * 60 * 2 => '2 Stunden',
  60 * 60 * 2 + 60 * 30 => '2 Stunden 30 Minuten',
  60 * 60 * 3 => '3 Stunden'
  /* usw */
);

if (isset($_POST['team_daily']) && isset($aOptions[$_POST['team_daily']])) {
  echo $aOptions[$_POST['team_daily']];
}

?>
<form action="..." method="post">
  <select name="team_daily">
<?php foreach ($aOptions as $iValue => $sRepresentation) : ?>
    <option value="<?= $iValue ?>"><?= $sRepresentation ?></option>
<?php endforeach; ?>
  </select>
</form>

Ps: Dein Fehler ist das du <option id=... geschrieben hast anstadt <option value=...
Pps: Ich würde es aber eher so schreiben wie ich es gerade getan habe ;P
 
PHP:
<?php
$aOptions = array(
  60 * 60 * 2 => '2 Stunden',
  60 * 60 * 2 + 60 * 30 => '2 Stunden 30 Minuten',
  60 * 60 * 3 => '3 Stunden'
  /* usw */
);

if (isset($_POST['team_daily']) && isset($aOptions[$_POST['team_daily']])) {
  echo $aOptions[$_POST['team_daily']];
}

?>
<form action="..." method="post">
  <select name="team_daily">
<?php foreach ($aOptions as $iValue => $sRepresentation) : ?>
    <option value="<?= $iValue ?>"><?= $sRepresentation ?></option>
<?php endforeach; ?>
  </select>
</form>

Ps: Dein Fehler ist das du <option id=... geschrieben hast anstadt <option value=...
Pps: Ich würde es aber eher so schreiben wie ich es gerade getan habe ;P

ah okay danke :DD
Hat geklappt :)
 
Hätte noch ne Frage :)

Wenn ich das jetzt abgesendet habe das Formular, möchte ich das genau das option Feld "selected" ist, welches ich beim Absenden des Formulars ausgewählt habe.

Wenn das formular noch nicht abgesendet wurde, so soll kein "selected" feld sein.

PHP:
          <select style="width:140px; " class="mbo_input" name="team_daily">
  <?php if(isset($_POST['team_daily'])) {?>
  <?php foreach ($aOptions as $iValue => $sRepresentation) { ?>
  <option <?php if($_POST['team_daily'] == $iValue){echo 'selected';}?> value="<?= $iValue ?>"><?= $sRepresentation ?></option>
  <?php ;}
  } else { ?>
  <?php foreach ($aOptions as $iValue => $sRepresentation) { ?>
  <option value="<?= $iValue ?>"><?= $sRepresentation ?></option>
  <?php ;}
  ;}?>
  </select>

*Edit*

Habs jetzt :)
Ist das ganze so in ordnung? Oder geht das auch anders? Ohne Javascript.

MFG
 
Zuletzt bearbeitet:
PHP:
<?php
$aOptions = array(
  60 * 60 * 2 => '2 Stunden',
  60 * 60 * 2 + 60 * 30 => '2 Stunden 30 Minuten',
  60 * 60 * 3 => '3 Stunden'
  /* usw */
);

$iSelectedItem = null;
if (isset($_POST['team_daily']) && isset($aOptions[$_POST['team_daily']])) {
  $iSelectedItem = (int)$_POST['team_daily'];
  echo $aOptions[$_POST['team_daily']];
}

?>
<form action="..." method="post">
  <select name="team_daily">
<?php foreach ($aOptions as $iValue => $sRepresentation) : ?>
    <option value="<?= $iValue ?>"<?= ($iValue === $iSelectedItem ? ' selected' : '') ?>><?= $sRepresentation ?></option>
<?php endforeach; ?>
  </select>
</form>
 
Zurück
Oben