PHP Werte in HTML input Anzeigen

  • Ok ich habe es jetzt so weit fertig. Vielen Dank!:bravo2:

    Und ja, ich benutze mehrere Formulare, um genau zu sein 5. Die layoutBox definiere ich jetzt auch mit der Id. Ich habe mal die Ersten beiden mit dem PHP Code vorweg hier:
    [code=php]<?php
    $id = 0;
    $days = array(
    "monday" => "Montag",
    "tuesday" => "Dienstag",
    "wednesday" => "Mittwoch",
    "thursday" => "Donnerstag",
    "friday" => "Freitag",
    "saturday" => "Samstag",
    "sunday" => "Sonntag");

    $CONFIG_FILENAME = '/var/www/data/alarmClock.xml';
    //config.xml dateisystem rechte überprüfen
    if(!file_exists($CONFIG_FILENAME)) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht finden!\n";
    exit(1);
    }
    if(!is_readable($CONFIG_FILENAME)) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht lesen!\n";
    exit(2);
    }
    if(!is_writable($CONFIG_FILENAME)) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht schreiben!\n";
    exit(3);
    }

    libxml_use_internal_errors(true);
    $xml = simplexml_load_file($CONFIG_FILENAME);
    if (!$xml) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht laden!\n";
    foreach(libxml_get_errors() as $error) {
    echo "\t", $error->message;
    }
    exit(4);
    }

    //xml schreiben
    if(isset($_POST['submit']))
    {
    if(isset($_POST['alarmAktiv'])) {
    $xml->{"alarm".$_POST['id']}->aktiv = "true";
    }
    else {
    $xml->{"alarm".$_POST['id']}->aktiv = "false";
    }

    if (isset($_POST['repeatAktiv'])) {
    $xml->{"alarm".$_POST['id']}->repeat = "true";
    }
    else {
    $xml->{"alarm".$_POST['id']}->repeat = "false";
    }

    foreach($days as $key => $value) {
    if(in_array($key, $_POST['day']))
    {
    $xml->{"alarm".$_POST['id']}->{$value} = "true";
    }
    else {
    $xml->{"alarm".$_POST['id']}->{$value} = "false";
    }
    }

    $xml->{"alarm".$_POST['id']}->time = $_POST['time'];

    $xml->{"alarm".$_POST['id']}->mode = $_POST['mode'];

    $dom = new DOMDocument('1.0');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($xml->asXML());
    $dom->save($CONFIG_FILENAME);
    }
    ?>

    <form action="" method='POST'>
    <div class="<?php echo 'layoutBox'.$id; ?>">
    <label class="alarmClockLabel" > Wecker 1:
    <input type="hidden" name="id" value="<?php echo $id;?>" >
    <input type="submit" class="saveButton" name="submit" value="Speichern" >
    <label class="labelAktiv" > Aktiv:
    <input type="checkbox" class="checkBox" name="alarmAktiv" <?php if((string)$xml->{"alarm".$id}->aktiv == "true") { echo 'checked="checked"'; } ?> >
    </label>
    <label class="labelRepeat" > Wiederholen:
    <input type="checkbox" class="checkBox" name="repeatAktiv" <?php if((string)$xml->{"alarm".$id}->repeat == "true") { echo 'checked="checked"'; } ?> >
    </label>
    <label class="labelDay" > Tag:
    <select name="day[]" class="choiceBox" multiple>
    <?php
    foreach($days as $key => $value)
    {
    echo '<option value="'.$key.'" ';
    if((string)$xml->{"alarm".$id}->{$value} == "true") {
    echo 'selected="selected"';
    }
    echo '>'.$value.'</option>\r\n';
    }
    ?>
    </select>
    </label>
    <label class="labelTime" > Zeit:
    <input type="time" class="timeBox" name="time" value="<?php echo (string)$xml->{"alarm".$id}->time; ?>">
    </label>
    <label class="labelMode" > Modus:
    <select name="mode" class="choiceBox" size="1" >
    <option value="standad" <?php if((string)$xml->{"alarm".$id}->mode == "standard") { echo 'selected="selected"'; } ?>>Standard</option>
    </select>
    </label>
    </label>
    </div>
    </form>

    <?php $id = 1; ?>
    <form action="" method='POST'>
    <div class="<?php echo 'layoutBox'.$id; ?>">
    <label class="alarmClockLabel" > Wecker 2:
    <input type="hidden" name="id" value="<?php echo $id;?>" >
    <input type="submit" class="saveButton" name="submit" value="Speichern" >
    <label class="labelAktiv" > Aktiv:
    <input type="checkbox" class="checkBox" name="alarmAktiv" <?php if((string)$xml->{"alarm".$id}->aktiv == "true") { echo 'checked="checked"'; } ?> >
    </label>
    <label class="labelRepeat" > Wiederholen:
    <input type="checkbox" class="checkBox" name="repeatAktiv" <?php if((string)$xml->{"alarm".$id}->repeat == "true") { echo 'checked="checked"'; } ?> >
    </label>
    <label class="labelDay" > Tag:
    <select name="day[]" class="choiceBox" multiple>
    <?php
    foreach($days as $key => $value)
    {
    echo '<option value="'.$key.'" ';
    if((string)$xml->{"alarm".$id}->{$value} == "true") {
    echo 'selected="selected"';
    }
    echo '>'.$value.'</option>\r\n';
    }
    ?>
    </select>
    </label>
    <label class="labelTime" > Zeit:
    <input type="time" class="timeBox" name="time" value="<?php echo (string)$xml->{"alarm".$id}->time; ?>">
    </label>
    <label class="labelMode" > Modus:
    <select name="mode" class="choiceBox" size="1" >
    <option value="standad" <?php if((string)$xml->{"alarm".$id}->mode == "standard") { echo 'selected="selected"'; } ?>>Standard</option>
    </select>
    </label>
    </label>
    </div>
    </form> [/php]

    :danke_ATDE:

  • mach mal so:
    [code=php]<?php
    $days = array(
    "monday" => "Montag",
    "tuesday" => "Dienstag",
    "wednesday" => "Mittwoch",
    "thursday" => "Donnerstag",
    "friday" => "Freitag",
    "saturday" => "Samstag",
    "sunday" => "Sonntag");

    $CONFIG_FILENAME = 'test1.xml';
    //config.xml dateisystem rechte überprüfen
    if(!file_exists($CONFIG_FILENAME)) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht finden!\n";
    exit(1);
    }
    if(!is_readable($CONFIG_FILENAME)) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht lesen!\n";
    exit(2);
    }
    if(!is_writable($CONFIG_FILENAME)) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht schreiben!\n";
    exit(3);
    }

    libxml_use_internal_errors(true);
    $xml = simplexml_load_file($CONFIG_FILENAME);
    if (!$xml) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht laden!\n";
    foreach(libxml_get_errors() as $error) {
    echo "\t", $error->message;
    }
    exit(4);
    }

    //xml schreiben
    if(isset($_POST['submit']))
    {
    if(isset($_POST['alarmAktiv'])) {
    $xml->{"alarm".$_POST['id']}->aktiv = "true";
    }
    else {
    $xml->{"alarm".$_POST['id']}->aktiv = "false";
    }

    if (isset($_POST['repeatAktiv'])) {
    $xml->{"alarm".$_POST['id']}->repeat = "true";
    }
    else {
    $xml->{"alarm".$_POST['id']}->repeat = "false";
    }

    foreach($days as $key => $value) {
    if(in_array($key, $_POST['day']))
    {
    $xml->{"alarm".$_POST['id']}->{$value} = "true";
    }
    else {
    $xml->{"alarm".$_POST['id']}->{$value} = "false";
    }
    }

    $xml->{"alarm".$_POST['id']}->time = $_POST['time'];

    $xml->{"alarm".$_POST['id']}->mode = $_POST['mode'];

    $dom = new DOMDocument('1.0');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($xml->asXML());
    $dom->save($CONFIG_FILENAME);
    }

    function show_alarm($alarm_ID)
    {
    global $xml;
    global $days;
    ?>
    <form action="" method='POST'>
    <div class="layoutBox">
    <label class="alarmClockLabel" > Wecker <?php echo $alarm_ID+1; ?>:
    <input type="hidden" name="id" value="<?php echo $alarm_ID;?>" >
    <input type="submit" class="saveButton" name="submit" value="Speichern" >
    <label class="labelAktiv" > Aktiv:
    <input type="checkbox" class="checkBox" name="alarmAktiv" <?php if((string)$xml->{"alarm".$alarm_ID}->aktiv == "true") { echo 'checked="checked"'; } ?> >
    </label>
    <label class="labelRepeat" > Wiederholen:
    <input type="checkbox" class="checkBox" name="repeatAktiv" <?php if((string)$xml->{"alarm".$alarm_ID}->repeat == "true") { echo 'checked="checked"'; } ?> >
    </label>
    <label class="labelDay" > Tag:
    <select name="day[]" class="choiceBox" multiple>
    <?php
    foreach($days as $key => $value)
    {
    echo '<option value="'.$key.'" ';
    if((string)$xml->{"alarm".$alarm_ID}->{$value} == "true") {
    echo 'selected="selected"';
    }
    echo '>'.$value.'</option>\r\n';
    }
    ?>
    </select>
    </label>
    <label class="labelTime" > Zeit:
    <input type="time" class="timeBox" name="time" value="<?php echo (string)$xml->{"alarm".$alarm_ID}->time; ?>">
    </label>
    <label class="labelMode" > Modus:
    <select name="mode" class="choiceBox" size="1" >
    <option value="standad" <?php if((string)$xml->{"alarm".$alarm_ID}->mode == "standard") { echo 'selected="selected"'; } ?>>Standard</option>
    </select>
    </label>
    </label>
    </div>
    </form>
    <br>
    <?php
    }

    echo show_alarm(0);
    echo show_alarm(1);
    ?>[/php]
    Sprich das Formular in eine Funktion.
    Damit ist das Formular beliebig oft vervielfältigen. (Durch die funktion)

    Die variable $id fällt dann oben auch weg

  • und gleich nochmal eine verbesserung!
    [code=php]<?php
    $days = array(
    "monday" => "Montag",
    "tuesday" => "Dienstag",
    "wednesday" => "Mittwoch",
    "thursday" => "Donnerstag",
    "friday" => "Freitag",
    "saturday" => "Samstag",
    "sunday" => "Sonntag");

    $CONFIG_FILENAME = 'test1.xml';
    //config.xml dateisystem rechte überprüfen
    if(!file_exists($CONFIG_FILENAME)) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht finden!\n";
    exit(1);
    }
    if(!is_readable($CONFIG_FILENAME)) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht lesen!\n";
    exit(2);
    }
    if(!is_writable($CONFIG_FILENAME)) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht schreiben!\n";
    exit(3);
    }

    libxml_use_internal_errors(true);
    $xml = simplexml_load_file($CONFIG_FILENAME);
    if (!$xml) {
    echo "Kann die Konfiguration (".$CONFIG_FILENAME.") nicht laden!\n";
    foreach(libxml_get_errors() as $error) {
    echo "\t", $error->message;
    }
    exit(4);
    }

    //xml schreiben
    if(isset($_POST['submit']))
    {
    if(isset($_POST['alarmAktiv'])) {
    $xml->{"alarm".$_POST['id']}->aktiv = "true";
    }
    else {
    $xml->{"alarm".$_POST['id']}->aktiv = "false";
    }

    if (isset($_POST['repeatAktiv'])) {
    $xml->{"alarm".$_POST['id']}->repeat = "true";
    }
    else {
    $xml->{"alarm".$_POST['id']}->repeat = "false";
    }

    foreach($days as $key => $value) {
    if(in_array($key, $_POST['day']))
    {
    $xml->{"alarm".$_POST['id']}->{$value} = "true";
    }
    else {
    $xml->{"alarm".$_POST['id']}->{$value} = "false";
    }
    }

    $xml->{"alarm".$_POST['id']}->time = $_POST['time'];

    $xml->{"alarm".$_POST['id']}->mode = $_POST['mode'];

    $dom = new DOMDocument('1.0');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($xml->asXML());
    $dom->save($CONFIG_FILENAME);
    }

    function show_alarm($alarm_ID)
    {
    global $xml;
    global $days;

    $alarm = $xml->{"alarm".$alarm_ID};
    ?>
    <form action="" method='POST'>
    <div class="layoutBox">
    <label class="alarmClockLabel" > Wecker <?php echo $alarm_ID+1; ?>:
    <input type="hidden" name="id" value="<?php echo $alarm_ID;?>" >
    <input type="submit" class="saveButton" name="submit" value="Speichern" >
    <label class="labelAktiv" > Aktiv:
    <input type="checkbox" class="checkBox" name="alarmAktiv" <?php if((string)$alarm->aktiv == "true") { echo 'checked="checked"'; } ?> >
    </label>
    <label class="labelRepeat" > Wiederholen:
    <input type="checkbox" class="checkBox" name="repeatAktiv" <?php if((string)$alarm->repeat == "true") { echo 'checked="checked"'; } ?> >
    </label>
    <label class="labelDay" > Tag:
    <select name="day[]" class="choiceBox" multiple>
    <?php
    foreach($days as $key => $value)
    {
    echo '<option value="'.$key.'" ';
    if((string)$alarm->{$value} == "true") {
    echo 'selected="selected"';
    }
    echo '>'.$value.'</option>\r\n';
    }
    ?>
    </select>
    </label>
    <label class="labelTime" > Zeit:
    <input type="time" class="timeBox" name="time" value="<?php echo (string)$alarm->time; ?>">
    </label>
    <label class="labelMode" > Modus:
    <select name="mode" class="choiceBox" size="1" >
    <option value="standad" <?php if((string)$alarm->mode == "standard") { echo 'selected="selected"'; } ?>>Standard</option>
    </select>
    </label>
    </label>
    </div>
    </form>
    <br>
    <?php
    }

    echo show_alarm(0);
    echo show_alarm(1);
    ?>[/php]

    Also in der funktion nach den globalen Variablen
    [code=php]$alarm = $xml->{"alarm".$alarm_ID};[/php]
    Somit kannst du viel einfacher einen Eintrag auslesen. Mit:
    [code=php]if((string)$alarm->aktiv == "true")[/php]


    EDIT:
    Dein Label über das komplette Formular ist übrigens ziemlich unglücklich!
    [code=php]<label class="alarmClockLabel" > Wecker <?php echo $alarm_ID+1; ?>:[/php]
    dadurch "zeigst" du quasi mit jedem Textfeld, checkbox und dropdown auf den "Speichern" button.
    Wich würd des Label über alles einfach weg lassen - wozu auch??

    EDIT2:
    und das xml schreiben auch optimiert:
    [code=php]//xml schreiben
    if(isset($_POST['submit']))
    {
    $alarm = $xml->{"alarm".$_POST['id']};

    if(isset($_POST['alarmAktiv'])) {
    $alarm->aktiv = "true";
    }
    else {
    $alarm->aktiv = "false";
    }

    if (isset($_POST['repeatAktiv'])) {
    $alarm->repeat = "true";
    }
    else {
    $alarm->repeat = "false";
    }

    foreach($days as $key => $value) {
    if(in_array($key, $_POST['day']))
    {
    $alarm->{$value} = "true";
    }
    else {
    $alarm->{$value} = "false";
    }
    }

    $alarm->time = $_POST['time'];

    $alarm->mode = $_POST['mode'];

    $dom = new DOMDocument('1.0');
    $dom->preserveWhiteSpace = false;
    $dom->formatOutput = true;
    $dom->loadXML($xml->asXML());
    $dom->save($CONFIG_FILENAME);
    }[/php]

  • Eine Sache ist mir noch augefallen. Der ändert den Tag Namen alarm in z.B. alarm0. Deshalb hatte ich grade probleme beim einlesen in python. ... Die XML sieht jetzt so aus:
    [code=php]<root>
    <alarm0>
    <aktiv>true</aktiv>
    <repeat>true</repeat>
    <Montag>false</Montag>
    <Dienstag>false</Dienstag>
    <Mittwoch>false</Mittwoch>
    <Donnerstag>false</Donnerstag>
    <Freitag>false</Freitag>
    <Samstag>false</Samstag>
    <Sonntag>false</Sonntag>
    <time/>
    <mode>standad</mode>
    </alarm0>
    <alarm1>
    <aktiv>false</aktiv>
    <repeat>false</repeat>
    <Montag>false</Montag>
    <Dienstag>false</Dienstag>
    <Mittwoch>false</Mittwoch>
    <Donnerstag>false</Donnerstag>
    <Freitag>false</Freitag>
    <Samstag>false</Samstag>
    <Sonntag>false</Sonntag>
    <time/>
    <mode>standad</mode>
    </alarm1>
    ...
    </root>[/php]

    Einmal editiert, zuletzt von BlueDogi (30. April 2015 um 12:55)

Jetzt mitmachen!

Du hast noch kein Benutzerkonto auf unserer Seite? Registriere dich kostenlos und nimm an unserer Community teil!