Control style whereby PHP

Hi everyone,
The following code displays a form to extract date from user and show the date within its’ div.
When the selected date is displayed, it is displayed above the form !
Why ?!
Question 1:
How do I place, via PHP, the selected date, within its’ div, beneath the form?
Question 2:
I’d like the text of the above mentioned div to be red. Can I add to .myDiv class a rule (or just change
The value) “(color: red ;)”. Can I do it whereby php code ?
Thanks a lot!

<?php
//form_date_to_php_div1.php
require_once '../myInitial.php';
require_once '../myLogin.php';
MYSQLI_SET_CHARSET($myConnection,'UTF8');
$myDate = time();
$myDate1 = date('Y-m-d  l', $myDate);

if (isset($_GET['dateToDiv']))
{
    $myDate1 = sanitize1($myConnection, $_GET['dateToDiv']);
    echo '<p class = "myDiv">'.$myDate1.'</p>'; 
}

function sanitize1($conn, $aDate)
{
    return htmlentities(sanitize2($conn, $aDate));
}
function sanitize2($conn, $aDate)
{
    if (get_magic_quotes_gpc()) $aDate = stripslashes($aDate);
    return $conn->real_escape_string($aDate);
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>form_date_tp_php_div.php</title>
    <style>
        .myDiv
        {
            display:block;
            width: 10%;
            height: 10%;
            border: 1px solid red;
        }
    </style>
</head>
<body>
    <form action = "form_date_to_php_div1.php" method = "GET">
        <input type = "date" name = "dateToDiv" />
        <input type="submit" value = "Send"/>
    </form>
</body>
</html>

You are echoing the date even before your doctype declaration which is clearly wrong. Your PHP code belongs in the body of your page, and if you want the date output after the form then place the PHP code after the form.

You would simply move the echo to the appropriate place in the html.

...
    </form>
   <p class="myDiv"><?php echo $myDate1 ?></p>
</body>
</html>
1 Like

I would prefer to keep the logic part of the php out of the html and limit it to only the echo, or the minimum which is required within the html.

3 Likes

Yes, I’ll drink to that!

2 Likes

Try returning a $result from the function instead of echo within the function, which is not good practice.

Within the body statements, echo function(…);

1 Like

Thank you Sam. I’ll adopt your advise.

Thank you gandalf.

Thanks.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.