0

I am trying to display a default value in input type date. The date i want do display is a date from database(format in database is 2016-05-07 --- y-m-d) and then i did change the format of date to dd.mm.yyyy and saved it in variable $DO. This part i did accomplish.

But the problem starts when transferring the value to input field type date. It doesn't display any data but when i echo the variable $DO it displays the correct date and format?

this is my code php code:

<section id="sadrzaj">

        <h1> Ažuriranje aktivnosti:
        </h1>

            <?php 
            $sql= "SELECT aktivnost.naziv NA, aktivnost.aktivnost_id AID , udruga.naziv UN, aktivnost.datum_odrzavanja DO, 
            aktivnost.vrijeme_odrzavanja VO, aktivnost.opis AO
            FROM aktivnost, udruga
            WHERE aktivnost.aktivnost_id='".$_GET['aktivnost_id']."' 
            AND udruga.moderator_id='".$_SESSION['id']."' 
            AND udruga.udruga_id=aktivnost.udruga_id ";


            $rezUpita = izvrsiU($v4,$sql);
            $rez3=mysqli_fetch_assoc($rezUpita);


        if ($rez = mysqli_num_rows($rezUpita) == 0){

            echo "Nemate aktivnosti.";
        }
        echo "<br/>";


        $DO = $rez3['DO'];
            $DO = str_replace('.','-', $DO);
            $DO= date("d.m.Y",strtotime($DO));

        echo $DO;
        ?>

    <form action="<?php echo $_SERVER["PHP_SELF"]. '?'.'aktivnost_id='.$_GET['aktivnost_id']; ?>"  method="POST">

        <label for="NA">Naziv aktivnosti: </label><br/>
            <input type="text" name="NA" value="<?php echo $rez3['NA']; ?>"><br><br>

            <label for="DO">Datum održavanja aktivnosti: </label><br/>

            <input type="date" name="DO" value="<?php echo $DO; ?>"><br><br>



            <label for="VO">Vrijeme održavanja aktivnosti: </label><br/>
            <input type="time" name="VO" value="<?php echo $rez3['VO']; ?>"><br><br>

            <label for="AO">Opis aktivnosti: </label><br/>
            <textarea name="AO" id="AO" maxlength="1000" rows="5" cols="100">
             <?php echo $rez3['AO']; ?>
             </textarea><br/><br>


             <input type="submit" name="UpA" value="Ažuriraj"><br><br>

             </form>


        </section>

Screenshoot of html inspector shoes that the correct date is in value but it isnt displayed .html inspector

robi10
  • 3
  • 7
  • Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use prepared statements with bound parameters, via either the [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) drivers. [**This post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has some good examples. – Alex Howansky Jun 23 '17 at 16:10

1 Answers1

0

There is a problem with the date format you are using.

You should use one of the following formats:

  • dd/mm/yyyy
  • mm/dd/yyyy
  • dd-mm-yyyy
  • mm-dd-yyyy

If you want to use your format (dd.mm.yyyy) you will need to use some javascript, or just use a datepicker that does this for you :) For example: https://eonasdan.github.io/bootstrap-datetimepicker/

More about date input type: https://www.w3.org/wiki/HTML/Elements/input/date

Jan Vidic
  • 134
  • 9