Ankündigung

Einklappen
Keine Ankündigung bisher.

Vorauswahl beihalten in <selected><option>

Einklappen

Neue Werbung 2019

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

  • Vorauswahl beihalten in <selected><option>

    Hallo liebe PHP User

    Ich habe folgendes Problem:
    Ich habe eine User-Profilseite gemacht und die beinhaltet ein Funktion zur Angabe des Geburtsdatums.

    Soweit funktioniert es auch. Der User wählt sein Datum und es wird als Varchar in die Datenbank geschrieben.


    screen1:



    wenn der User aber wiederholt das Profil ändert und vergisst sein Geburtsdatum wieder zu selektieren, dann wird es in der Datenbank überschrieben.



    Ich möchte also das sein Geburtsdatum wenn er aufs Profil geht vor selektiert
    wird so wie es in der DB eingetragen ist.


    Ich poste beabsichtigt nur Teilcodes da es sonst diesen Post sprengen würde.
    Wenn aber benötigt werde ich den kompletten Code hier posten.


    und hoffe, dass mir jemand helfen kann.

    mfg
    Wm2k6

    Code zur Profilseite:
    Code:
            <td align="center">Geburtsdatum: <span class="change">
        
        
        
    
    
    PHP-Code:
    <?php echo $row1['geboren']; ?>
    </span><br /> <p style="margin-top:5px;"><span class="infotext">Tag:</span> <select name="tag" id="tag"
    PHP-Code:
    <?php echo "'".$tag."'"?>
    > <option value="-1" ></option> <option value="01" >1</option> <option value="02" >2</option> <option value="03" >3</option> <option value="04" >4</option> <option value="05" >5</option> <option value="06" >6</option> <option value="07" >7</option> <option value="08" >8</option> <option value="09" >9</option>
    PHP Code Variablen!
    PHP-Code:
    $tag $_POST['tag'];
    $monat $_POST['monat'];
    $jahr $_POST['jahr']; 
    $geboren=htmlspecialchars($_POST['geboren']); 
    PHP Code zum Eintrag in die Datebank!
    PHP-Code:
    if(mysql_query("update profil set icq='$icq',msn='$msn',aim='$aim',yahoo='$yahoo',skype='$skype',webseite='$webseite',irc='$irc',ircs='$ircs',ircse='$ircse',geschlecht='$sex',geboren='".$tag.".".$monat.".".$jahr."',hobbys='$hobbys',ip='$ip',date='$date' where user_id='$userid'")) 

  • #2
    na zerlege doch $row1['geboren'] in seine BEstandteile und mache in deinen dropdowns ein selected, wenn value dem wert entspricht, fertig

    Kommentar


    • #3
      btw scheinst Du das Datum in deiner DB im nicht im datumsformat vorzuhalten -> das is nich gut

      Kommentar


      • #4
        zum Beispiel

        PHP-Code:
        <option value="12" <? if($monat==12) echo 'selected="selected"'; ?>>12</option>

        Kommentar


        • #5
          und einfach hast Du es auch wenn Du so dropdowns wie Du sie postest in ner schleife generierst.

          Kommentar


          • #6
            @cycap
            Leider funktioniert der Code nicht.
            PHP-Code:
            <option value="12" <? if($monat==12) echo 'selected="selected"'; ?>>12</option>
            @HPR1974
            Leider bin ich noch nicht soweit in PHP und weiß wirklich nicht, wie man das realisiert.
            Es wäre lieb, wenn es nicht soviel Mühe macht mir so eine Schleife zu schreiben.
            Code:
            na zerlege doch $row1['geboren'] in seine BEstandteile und mache in deinen dropdowns ein selected, wenn value dem wert entspricht, fertig
            Das habe ich versucht mit list und dann explode aber dann kommt als Ergebnis
            immer nur Punkte.

            Code:
            btw scheinst Du das Datum in deiner DB im nicht im datumsformat vorzuhalten -> das is nich gut
            Die Datenbank kann ich ja umstellen auf Date.
            Aber dann bin ich immer noch nicht weiter.

            mfg
            Wm2k6

            Kommentar


            • #7
              Guckst Du hier: Affenformular (Standardverfahren) - PHP.de Wiki

              Kommentar


              • #8
                PHP-Code:
                <select name="monat" id="monat" >
                              <option value="-1" ></option>
                              <option value="Januar"<?php if ('Januar' == $_POST['monat']){echo 'selected="selected"';}?>>Januar</option>
                              <option value="Februar"<?php if ('Februar' == $_POST['monat']){echo 'selected="selected"';}?>>Februar</option>
                              <option value="März">März</option>
                              <option value="April">April</option>
                Das gleiche habe ich für Tag und Jahr gemacht.
                Leider funzt es so auch nicht. :-/

                mfg
                Wm2k6

                Kommentar


                • #9
                  Für jeden Monat, also nicht nur Januar und Februar? Das ist eine ganze Menge Tipparbeit, die Du Dir ersparen solltest.

                  Erstmal solltest Du überprüfen, ob $_POST['monat'] überhaupt gesetzt ist und einen "gültigen" Wert besitzt. Wenn nicht, musst Du den Wert aus der Datenbank benutzen.

                  Und dann ist es wesentlich einfacher, wenn Du ein Feld der Art $monate = array( 'Januar', 'Februar', ... 'Dezember'); und foreach benutzt.
                  PHP-Code:
                  echo '<option value="-1" ></option>';
                  foreach(
                  $monate as $m) {
                      echo 
                  '<option', ($m==$vergleichswert) ? ' selected="selected">' '>'
                          
                  htmlentities($m),
                          
                  '</option>'
                      
                  ;

                  Kommentar


                  • #10
                    Hallo David

                    ich verzweifel bald

                    Also ich bekomme nur das Geburtsdatum angezeigt durch
                    PHP-Code:
                    <?php echo $row1['geboren']; ?>
                    wenn ich aber folgendes mache
                    PHP-Code:
                    $tag = $_POST['tag'];
                    <?php echo $row1['geboren']; ?><?php echo $tag?>
                    wird mein Tag auch nicht angezeigt.
                    Der Datenbankeintrag ist wie folgt.
                    PHP-Code:
                     if(mysql_query("update profil set icq='$icq',msn='$msn',aim='$aim',yahoo='$yahoo',skype='$skype',webseite='$webseite',irc='$irc',ircs='$ircs',ircse='$ircse',geschlecht='$sex',geboren='".$tag.".".$monat.".".$jahr."',hobbys='$hobbys',ip='$ip',date='$date' where user_id='$userid'")) 
                    und somit wird ja auch nur Table-row 'geboren' ausgelesen.
                    Ich habe mit list und explode versucht es aufzuspalten aber ohne Erfolg.
                    Ergebnis waren ein paar Punkte die mir angezeigt wurden.

                    Was soll ich jetzt noch machen?

                    mfg
                    Wm2k6

                    Kommentar


                    • #11
                      Also erstmal würde ich den Feldtyp in der Datenbank auf einen Datumstyp umstellen (um mich dann von Puristen zerfleischen zu lassen ).
                      Dann kannst Du auf der einen Seite innerhalb von MySQL mit dem Wert "rechnen", sprich die Datumsfunktionen verwenden, und auf der anderen Seite bei Bedarf die Einzelteile abrufen
                      Code:
                      SELECT
                        Year(geboren) as y, Month(geboren) as m, Day(geboren) as m
                      ...
                      .
                      Wieder einfügen kannst Du den Wert dann als Zeichenkette im Format 'YYYY-MM-DD', also zum Beispiel
                      Code:
                      UPDATE ... geboren='1955-12-31'

                      Kommentar


                      • #12
                        also muss ich eine separat query machen da ich ja schon am Anfang meiner Seite einen query ausführe siehe: Ich poste mal die ganze Seite.

                        Ich hoffe das bringt mehr Licht in die Sache.

                        PHP-Code:
                        <?PHP
                        $todo
                        =htmlspecialchars($_POST['todo']);
                        $icq=htmlspecialchars($_POST['icq']);
                        $msn=htmlspecialchars($_POST['msn']);
                        $aim=htmlspecialchars($_POST['aim']);
                        $yahoo=htmlspecialchars($_POST['yahoo']);
                        $skype=htmlspecialchars($_POST['skype']);
                        $webseite=htmlspecialchars($_POST['webseite']);
                        $irc=htmlspecialchars($_POST['irc']);
                        $ircs=htmlspecialchars($_POST['ircs']);
                        $ircse=htmlspecialchars($_POST['ircse']);
                        $sex=$_POST['sex'];
                        $tag $_POST['tag'];
                        $monat $_POST['monat'];
                        $jahr $_POST['jahr']; 
                        $geboren=htmlspecialchars($_POST['geboren']);
                        $hobbys=htmlspecialchars($_POST['hobbys']);
                        $pseudo "avatar/blankoavatar.jpg";
                        $time=time();
                        $date date("d F Y"." "."H:i",$time);
                        $ip=$_SERVER['REMOTE_ADDR'];
                        $userid =$_SESSION[uid];
                        $sql1 "SELECT * FROM profil WHERE `user_id`='$userid'";
                                
                        $res1 mysql_query($sql1) or die(mysql_error());
                                
                        $row =  mysql_num_rows($res1);
                                
                        $row1 mysql_fetch_assoc($res1);
                                
                        if(isset(
                        $todo) and $todo=="update-profil"){
                        $status "OK";
                        $msg="";
                        ///// Selected
                        $sel['tag'] = array(
                                                        
                        '01' => '1' ,
                                                        
                        '02' => '2'
                                                        
                        );
                        $sel['tag'][$_POST['tag']] = 'selected="selected"';

                        /////Selected ende
                         
                        echo "<fieldset><legend class=\"legend\"> Meldung </legend>\n";
                        if(
                        $status<>"OK"){ // if validation failed
                        echo "<font face='Verdana' size='2' color=red>$msg</font><br><input type='button' value='Retry' onClick='history.go(-1)'>";
                        echo 
                        "</fieldset>";

                        }else{ 
                        // if all validations are passed.
                        if(mysql_query("update profil set icq='$icq',msn='$msn',aim='$aim',yahoo='$yahoo',skype='$skype',webseite='$webseite',irc='$irc',ircs='$ircs',ircse='$ircse',geschlecht='$sex',geboren='".$tag.".".$monat.".".$jahr."',hobbys='$hobbys',ip='$ip',date='$date' where user_id='$userid'")){
                        echo 
                        "<font face='Verdana' size='2' color=green>Dein Profil wurde erfolgreich geändert.<br></font>";
                        print 
                        "<script>";
                        print 
                        " self.location='index.php?section=profil';";
                        print 
                        "</script>"
                        echo 
                        "</fieldset>";
                        }else{echo 
                        "<font face='Verdana' size='2' color=red>Fehler: Bitte kontaktiere den Webmaster.<br></font>";}
                        echo 
                        "</fieldset>";
                        }}
                        include (
                        'avatar.php');
                        ?>

                           <form action="index.php?section=profil" method="post" name="pro"><input type=hidden name=todo value=update-profil>
                            <fieldset>
                            <legend class="legend"> Instant Messengers </legend>
                            <table border="0" cellspacing="5" cellpadding="2" width="100%">
                              <tr>
                                <td align="right">ICQ Nummer:</td>
                                <td width="10%"><input name="icq" type="text" value="<?php echo $row1['icq']; ?>"/></td>
                                <td align="right" style="text-transform:none; color:#990033;">Eingetragen:</td>
                                <td align="left" style=" color:#3399CC; width:auto; text-transform:none; font-weight:normal;"><span style="margin-left:5px; "><?php echo $row1['icq']; ?></span></td>
                              </tr>
                              <tr>   
                                <td align="right">MSN Name:</td>
                                <td><input name="msn" type="text" value="<?php echo $row1['msn']; ?>"/></td>
                                <td align="right" style="text-transform:none; color:#990033;">Eingetragen:</td>
                                <td align="left" style=" color:#3399CC; width:auto; text-transform:none; font-weight:normal;"><span style="margin-left:5px; "><?php echo $row1['msn']; ?></span></td>
                              </tr>
                              <tr>
                                <td align="right">AIM Name:</td>
                                <td><input name="aim" type="text" value="<?php echo $row1['aim']; ?>"/></td>
                                <td align="right" style="text-transform:none; color:#990033;">Eingetragen:</td>
                                <td align="left" style=" color:#3399CC; width:auto; text-transform:none; font-weight:normal;"><span style="margin-left:5px; "><?php echo $row1['aim']; ?></span></td>
                             </tr>
                              <tr>   
                                <td align="right">Yahoo Name:</td>
                                <td><input name="yahoo" type="text" value="<?php echo $row1['yahoo']; ?>"/></td>
                                <td align="right" style="text-transform:none; color:#990033;">Eingetragen:</td>
                                <td align="left" style=" color:#3399CC; width:auto; text-transform:none;font-weight:normal;"><span style="margin-left:5px; "><?php echo $row1['yahoo']; ?></span></td>
                              </tr>
                              <tr>
                                <td align="right">Skype Name:</td>
                                <td><input name="skype" type="text" value="<?php echo $row1['skype']; ?>"/></td>
                                <td align="right" style="text-transform:none; color:#990033;">Eingetragen:</td>
                                <td align="left" style=" color:#3399CC; width:auto; text-transform:none;font-weight:normal;"><span style="margin-left:5px; "><?php echo $row1['skype']; ?></span></td>
                              </tr>
                            </table>
                            </fieldset>
                            <fieldset>
                            <legend class="legend"> Homepage </legend>
                            <table border="0" cellspacing="5" cellpadding="2" width="100%">
                              <tr>
                                <td align="right">Homepage:</td>
                                <td align="left"><input name="webseite" type="text" value="<?php echo $row1['webseite']; ?>"/></td>
                                <td align="right" style="text-transform:none; color:#990033;">Eingetragen:</td>
                                <td align="left" style=" color:#3399CC; width:auto; text-transform:none;font-weight:normal; font-size:9px;"><span style="margin-left:5px; "><a href="<?php echo $row1['webseite']; ?>"><?php echo $row1['webseite']; ?></a></span></td>
                              </tr>
                            </table>
                            </fieldset>
                            <fieldset>
                            <legend class="legend"> Irc </legend>
                            <table border="0" cellspacing="5" cellpadding="2" width="100%">
                              <tr>
                                <td align="right">IRC - Nick:</td>
                                <td><input name="irc" type="text" value="<?php echo $row1['irc']; ?>"/></td>
                                <td align="right" style="text-transform:none; color:#990033;">Eingetragen:</td>
                                <td align="left" style=" color:#3399CC; width:auto; text-transform:none;font-weight:normal;"><span style="margin-left:5px; "><?php echo $row1['irc']; ?></span></td>
                                </tr>
                                <tr>
                                <td align="right">IRC - Chan:</td>
                                <td><input name="ircs" type="text" value="<?php echo $row1['ircs']; ?>"/></td>
                                <td align="right" style="text-transform:none; color:#990033;">Eingetragen:</td>
                                <td align="left" style=" color:#3399CC; width:auto; text-transform:none;font-weight:normal;"><span style="margin-left:5px; "><?php echo $row1['ircs']; ?></span></td>
                              </tr>
                                <tr>
                                <td align="right">IRC - Server:</td>
                                <td><input name="ircse" type="text" value="<?php echo $row1['ircse']; ?>"/></td>
                                <td align="right" style="text-transform:none; color:#990033;">Eingetragen:</td>
                                <td align="left" style=" color:#3399CC; width:auto; text-transform:none;font-weight:normal;"><span style="margin-left:5px; "><a href="<?php echo $row1['ircse']; ?>"><?php echo $row1['ircse']; ?></a></span></td>
                              </tr>
                            </table>
                            </fieldset>
                            <fieldset>
                            <legend class="legend"> Geburtsdatum und Geschlecht </legend>
                            <table border="0" cellspacing="5" cellpadding="2" width="100%">
                              <tr>
                                <td align="right">Geschlecht: <span class="change"><?php echo $row1['geschlecht']; ?></span><br />
                                  <p style="margin-top:5px;">
                                    <? if($row1['geschlecht'] == "Mann"){
                        echo "<input type='radio' value=Mann checked name='sex' checked><span class=\"infotext\"> Mann <input type='radio' value=Frau  name='sex'> Frau";}
                        else {echo "<input type='radio' value=Mann checked name='sex' > Mann
                        <input type='radio' value=Frau  name='sex' checked> Frau";}?>
                                    </span></p></td>
                                <td align="center">&nbsp;</td>
                                <td align="center">Geburtsdatum: <span class="change"><?php echo $row1['geboren']; ?><?php echo $tag?></span><br />
                                  <p style="margin-top:5px;"><span class="infotext">Tag:</span>
                                    <select name="tag" id="tag">
                                      <option value="-1" ></option>
                                      <option value="01"<?php echo $sel['tag']['01']; ?>>1</option>
                                      <option value="02"<?php echo $sel['tag']['02']; ?>>2</option>
                                      <option value="03" >3</option>
                                      <option value="04" >4</option>
                                      <option value="05" >5</option>
                                      <option value="06" >6</option>
                                      <option value="07" >7</option>
                                      <option value="08" >8</option>
                                      <option value="09" >9</option>
                                      <option value="10" >10</option>
                                      <option value="11" >11</option>
                                      <option value="12" >12</option>
                                      <option value="13" >13</option>
                                      <option value="14" >14</option>
                                      <option value="15" >15</option>
                                      <option value="16" >16</option>
                                      <option value="17" >17</option>
                                      <option value="18" >18</option>
                                      <option value="19" >19</option>
                                      <option value="20" >20</option>
                                      <option value="21" >21</option>
                                      <option value="22" >22</option>
                                      <option value="23" >23</option>
                                      <option value="24" >24</option>
                                      <option value="25" >25</option>
                                      <option value="26" >26</option>
                                      <option value="27" >27</option>
                                      <option value="28" >28</option>
                                      <option value="29" >29</option>
                                      <option value="30" >30</option>
                                      <option value="31" >31</option>
                                    </select>
                                    <span class="infotext" style="text-transform:none;">Monat:</span>
                                    <select name="monat" id="monat" >
                                      <option value="-1" ></option>
                                      <option value="Januar"<?php if ('Januar' == $_POST['monat']){echo 'selected="selected"';}?>>Januar</option>
                                      <option value="Februar"<?php if ('Februar' == $_POST['monat']){echo 'selected="selected"';}?>>Februar</option>
                                      <option value="März">März</option>
                                      <option value="April">April</option>
                                      <option value="Mai">Mai</option>
                                      <option value="Juni">Juni</option>
                                      <option value="Juli">Juli</option>
                                      <option value="August">August</option>
                                      <option value="September">September</option>
                                      <option value="Oktober">Oktober</option>
                                      <option value="November">November</option>
                                      <option value="Dezember">Dezember</option>
                                    </select>
                                    <span class="infotext">Jahr:</span>
                                    <select name="jahr">
                                      <option value="-1"></option>
                                      <option value="1960"<?php if ('1960' == $_POST['jahr']){echo 'selected="selected"';}?>> 1960</option>
                                      <option value="1961"<?php if ('1961' == $_POST['jahr']){echo 'selected="selected"';}?>> 1961</option>
                                      <option value="1962"> 1962</option>
                                      <option value="1963"> 1963</option>
                                      <option value="1964"> 1964</option>
                                      <option value="1965"> 1965</option>
                                      <option value="1966"> 1966</option>
                                      <option value="1967"> 1967</option>
                                      <option value="1968"> 1968</option>
                                      <option value="1969"> 1969</option>
                                      <option value="1970"> 1970</option>
                                      <option value="1971"> 1971</option>
                                      <option value="1972"> 1972</option>
                                      <option value="1973"> 1973</option>
                                      <option value="1974"> 1974</option>
                                      <option value="1975"> 1975</option>
                                      <option value="1976"> 1976</option>
                                      <option value="1977"> 1977</option>
                                      <option value="1978"> 1978</option>
                                      <option value="1979"> 1979</option>
                                      <option value="1980"> 1980</option>
                                      <option value="1981"> 1981</option>
                                      <option value="1982"> 1982</option>
                                      <option value="1983"> 1983</option>
                                      <option value="1984"> 1984</option>
                                      <option value="1985"> 1985</option>
                                      <option value="1986"> 1986</option>
                                      <option value="1987"> 1987</option>
                                      <option value="1988"> 1988</option>
                                      <option value="1989"> 1989</option>
                                      <option value="1990"> 1990</option>
                                      <option value="1991"> 1991</option>
                                      <option value="1992"> 1992</option>
                                      <option value="1993"> 1993</option>
                                      <option value="1994"> 1994</option>
                                      <option value="1995"> 1995</option>
                                      <option value="1996"> 1996</option>
                                      <option value="1997"> 1997</option>
                                      <option value="1998"> 1998</option>
                                      <option value="1999"> 1999</option>
                                      <option value="2000"> 2000</option>
                                      <option value="2001"> 2001</option>
                                      <option value="2002"> 2002</option>
                                      <option value="2003"> 2003</option>
                                      <option value="2004"> 2004</option>
                                      <option value="2005"> 2005</option>
                                      <option value="2006"> 2006</option>
                                      <option value="2007"> 2007</option>
                                      <option value="2008"> 2008</option>
                                      <option value="2009"> 2009</option>
                                      <option value="2010"> 2010</option>
                                    </select>
                                  </p></td>
                              </tr>
                            </table>
                            </fieldset>
                            <fieldset>
                            <legend class="legend"> Ihre Hobbys </legend>
                            <table border="0" cellspacing="5" cellpadding="2" width="100%">
                              <tr>
                                <td align="center"><textarea cols="25" rows="6" name="hobbys"><?php echo $row1['hobbys']; ?></textarea></td>
                                <td  valign="top" align="left" style=" color:#3399CC; width:auto; text-transform:none;font-weight:normal;"><span style="margin-left:5px; "><?php         
                                $text 
                        $row1['hobbys']; 
                                
                        $neuertext wordwrap$text30"<br />\n\n" );
                                echo 
                        "<p align=\"left\">$neuertext</p>\n";
                                
                        ?></span></td>
                              </tr>
                            </table>
                            </fieldset>
                            <table border="0" cellspacing="5" cellpadding="2" width="100%">
                              <tr>
                                <td colspan="5" align="center"><input type="submit" value="Profil ändern"></td>
                              </tr>
                            </table>
                          </form>
                        </div>
                        </body>
                        </html>

                        Kommentar


                        • #13
                          Ähem in #8
                          <option value="Januar"<?php if ('Januar' == $_POST['monat']){echo 'selected="selected"';}?>>Januar</option>
                          einfach mal in den Quelltext schauen. Ich vermute es fehlt einfach nur ein Leerzeichen zwischen den Attributen.

                          Kommentar


                          • #14
                            Zitat von Wm2k6 Beitrag anzeigen
                            also muss ich eine separat query machen da ich ja schon am Anfang meiner Seite einen query ausführe
                            Nein, einfach hinzufügen.
                            Code:
                            SELECT
                             a,b,c,tralalla,Year(geboren) as y,Month(....

                            Kommentar


                            • #15
                              @nikosch

                              habe mal Leerzeichen eingefügt , leider ohne Erfolg.
                              PHP-Code:
                              <option value="Januar" <?php if ('Januar' == $_POST['monat']){echo 'selected="selected"';}?> >Januar</option>
                              @ David

                              So ich habe jetzt die DB auf DATE umgestellt.

                              und der SELECT Befehl wird schon oben als SELECT * FROM ausgeführt
                              also werde ich dann den umschreiben müssen.

                              PHP-Code:
                              $sql1 "SELECT user_id, msn, aim, yahoo, skype,webseite, irc, ircs, ircse, geschlecht, Year(geboren) as y, Month(geboren) as m, Day(geboren) as m, avatar, hobbys FROM profil WHERE `user_id`='$userid'"
                              Ok gemacht sag wie es weiter gehen soll

                              mfg
                              Wm2k6

                              Kommentar

                              Lädt...
                              X