samke
samke

Reputation: 17

How to show previous days as read only?

Can someone pls look into code below and tell me what am i doing wrong. What i am trying to accomplish is to make previous days read-only,that cant be change if is in past and when time given is also passed.Data is saved in mysql table (sorry for my poor english ) Thank you

<?php 
require_once '../../configuratie.php'; 

include template. 'header.php';

date_default_timezone_set('Europe/Amsterdam');
$time=date('Hi'); 
$datum = isset($_GET['datum']) ? $_GET['datum'] : date('Y-m-d ');
$prevDay = date('Y-m-d', strtotime($datum .' -1 day'));
$currentDay = date('Y-m-d H:i:s', strtotime($datum .' now'));
$nextDay = date('Y-m-d', strtotime($datum .' +1 day'));

$row = $conn->prepare(" SELECT * FROM tabelPL WHERE aangemaakt >= '".$currentDay."' order by aangemaakt ");
$row->execute();
$row = $row->fetch(PDO::FETCH_ASSOC);

if(isset($_POST['submit_ochtend'])) {   
    $P20 = filter_input(INPUT_POST, 'P20', FILTER_SANITIZE_STRING);

Upvotes: 1

Views: 92

Answers (1)

Nosajimiki
Nosajimiki

Reputation: 1103

You main problem appears to be here:

<?php if ( ($time >= "0600") && ($time <= "1400") === $prevDay)  : ?>

($time <= "1400") will evaluate as True or False, which you are trying to compare to a date.

Instead $currentDay needs to be evaluated separately against the server date like this.

<?php if ( ($time >= "0600") && ($time <= "1400") && date("Y-m-d") == $currentDay)  : ?>

Also, you don't need to make that acception for !empty($row['P20']. Placeholder Text only shows up when there is not content; so, you can always include it, and the browser will figure out if you need it. So, your whole output can be simplified to look like:

<?php 
    if (($time >= "0600") && ($time <= "1400") && (date("Y-m-d") == $currentDay)) {    
        echo'<textarea id="P20_ocht" name="P20" class="form-control animated" placeholder="Type hier uw tekst">'.$row['P20'].'</textarea>';
    } else {
        echo'<textarea id="P20_ocht" name="P20" class="form-control animated" placeholder="Type hier uw tekst" readonly>'.$row['P20'].'</textarea>';
    } 
?>

Last thing to consider is security. Just because you add a readonly tag does not mean someone can't edit the HTML in their browser to remove it; so, if this is a concern, you want to also make an exception in your PHP or SQL to throw out UPDATE requests for past dates which might look something like this:

$update_query = "UPDATE tabelPL SET P20 = ?, aangemaakt = NOW() WHERE id = ? AND aangemaakt >= ? LIMIT 1"; 
$stmt = $conn->prepare($update_query);
    $stmt->bindParam(1, $P20, PDO::PARAM_STR);
    $stmt->bindParam(2, $id, PDO::PARAM_INT);
    $stmt->bindParam(3, $currentDay, PDO::PARAM_STR);
    $stmt->execute();

Upvotes: 1

Related Questions