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

<select name = "xyz"> verliert gewählten Wert

Andreas200

Neues Mitglied
Hallo Zusammen !

Sicher ist es simpel, ich krieg es aber nicht hin. :(
Die erste Selectbox "jahr" ist zur Auswahl des Jahres. Nach dem klick auf OK1 soll die zweite Selectbox "tage" gefüllt werden. Das klappt auch alles wunderbar. Aber die Selectbox "jahr" behält nicht den ausgewählten Wert.
Ich wähle in Selectbox "jahr" 2009 ->klicke OK1 -> Selectbox "tage" wird ordnungsgemäß gefüllt und Selectbox "jahr" springt auf 2010 um und behält nicht den Wert 2009.

Wär supi wenn mir jemand helfen könnte ??

PHP:
<html>
<body>
<form method="post">
<?php
echo "<select name='jahr'>";

echo "<option>2010</option>";
echo "<option>2009</option>";
echo "<option>2008</option>";

echo "</select>";
echo "<input type='submit' name='ok1' value='OK'>";


if(isset($_POST["ok1"]))
{
include("script2.php");

$tag = mysql_query("select tag from spt where tag LIKE '%" . $_POST["jahr"] . "%'  order by tag desc");
   $num = mysql_num_rows($tag);

echo "<select name='tage'>";
 while ($dsatz = mysql_fetch_assoc($tag))
   {
       echo "<option>" . $dsatz['tag'] . "</option>";
   }
   echo "</select>";
}
?>
</form>
</body>
</html>
Danke Andreas
 
Im prinzip sollte dies gehen:
PHP:
<html>
<body>
<form method="post">
<?php
echo "<select name='jahr'>";

echo "<option";
if($_POST['jahr'] == 2010 && isset($_POST['ok1'])) { echo " SELECTED"; }
echo ">2010</option>";
echo "<option";
if($_POST['jahr'] == 2009 && isset($_POST['ok1'])) { echo " SELECTED"; }
echo ">2009</option>";
echo "<option";
if($_POST['jahr'] == 2008 && isset($_POST['ok1'])) { echo " SELECTED"; }
echo ">2008</option>";

echo "</select>";
echo "<input type='submit' name='ok1' value='OK'>";


if(isset($_POST["ok1"]))
{
include("script2.php");

$tag = mysql_query("select tag from spt where tag LIKE '%" . $_POST["jahr"] . "%'  order by tag desc");
   $num = mysql_num_rows($tag);

echo "<select name='tage'>";
 while ($dsatz = mysql_fetch_assoc($tag))
   {
       echo "<option>" . $dsatz['tag'] . "</option>";
   }
   echo "</select>";
}
?>
</form>
</body>
</html>
 
zur eigenen sicherheit solltest du optionsgruppen, die nicht auf namen reagieren können, exakt definieren:

HTML:
<option value="val">def</option>

Nils aka XraYSoLo
 
Bitte SQL-Injections vorbeugen und EVA-Prinzip beachten.

PHP:
<?php

print_r($_POST);

$tpl = array();
$tpl['step']  = 1;
$tpl['year']  = -1;
$tpl['years'] = range(2008, 2010);
$tpl['days']  = array();

if (isset($_POST['ok1'])) {
    if (isset($_POST['jahr'])) {
        $tpl['year'] = $_POST['jahr'];
    } else {
        die('Feld "jahr" nicht gesetzt.');
    }

    $tpl['step'] = 2;

    /*
    $res = mysql_query("
        SELECT
            `tag`
        FROM
            `spt`
        WHERE
            `tag` LIKE '%" . mysql_real_escape_string($_POST['jahr']) . "%'
        ORDER BY
            `tag` DESC"
    );

    $num = mysql_num_rows($tag);

    while ($row = mysql_fetch_assoc($res)) {
        $tpl['days'][] = $row;
    }
    */

    // Debug (ich habe die entsprechende Datenbanktabelle nicht
    foreach (range(1, 31) as $i) {
        $tpl['days'][] = array('tag' => $i);
    }
}

// Ab hier nur Werte aus $tpl verwenden

?>

<form method="post">
<p>
    <select name="jahr">
        <?php foreach ($tpl['years'] as $year) : ?>
            <option value="<?php echo htmlspecialchars($year); ?>"<?php
                if ($year == $tpl['year']) echo ' selected="selected"';
            ?>><?php echo htmlspecialchars($year) ?></option>
        <?php endforeach; ?>
    </select>
    <input type="submit" name="ok1" value="OK" />
</p>

<?php if ($tpl['step'] == 2) :
    /* include('script2.php'); */?>
<p>
    <select name="tage">
        <?php foreach ($tpl['days'] as $e) : ?>
        <option value="<?php echo htmlspecialchars($e['tag']); ?>"><?php
            echo htmlspecialchars($e['tag']); ?></option>
        <?php endforeach; ?>
    </select>
</p>
<?php endif; ?>

</form>

PS: Die SQL-Abfrage ist seltsam.
 
Zurück
Oben