简体   繁体   中英

Display current date as default in html/php form when window loads

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Smart Hotel - Check In Report</title>
<link rel="stylesheet" type="text/css" href="xxxxindex.css"/>
<link rel="stylesheet" type="text/css" href="xxxxform.css"/>
<script>
window.onload = function()
{
defaultDate();
}
function defaultDate()
{
var today = new Date();
document.getElementById("displayDate").value = [today.getDate(), today.getMonth()+1,  .getFullYear()].join('/');}
</script>   
</head>
<body>
<div id="screen"> 
<div id="wrapper">
<div class="roundContainer" id="menuBar">
<?php include("./menu.php")?>
</div> <!-- topContainer -->
<div class="roundContainer" id="main">
<form action="" method="Post" onsubmit="return confirm('Are you sure you want to display Check Ins?')">
<div id="formWrapper">
<h3>Check In Report</h3>
<div class="textInput">
<fieldset>
<label for="date">Display Report Since: </label>                
<input type="date" id = "displayDate" style="cursor:pointer;" /><br>
</fieldset>
</div>          
<div class="subInput">
<fieldset>
<input type="reset" value="Clear" name="clearB" style="width: 90px;cursor:pointer;">
<input type="submit" value="Display" name="submitB" style="width: 90px;cursor:pointer;">
</fieldset>
</div></div></form></div> <!-- formWrapper --></div> <!-- mainContainer -->
</div> <!-- Wrapper -->
<div id="userMenu">
<?php include("./userMenu.php")?>
</div> <!-- userMenu -->
</div> <!-- Screen -->
</body>
</html>

I have tried so many combinations, tried PhP functions and echo in the value tag inside the input, tried the

onload ="defaultDate()"

inside the tag, but no matter what I do, I keep getting nothing, just the normal dd/mm/yyyy in the date box.

I would appreciate any help, I have looked into all the answers given before to similar problems and none of them actually works.

Thank you.

If you havn't particular needs, you should launch the function when the page loads (with window.onload). I've also edited a bit your function to show the date in the dd/mm/yyyy format. Here's the code:

window.onload = function(){
    defaultDate()
};

function defaultDate()
{
var today = new Date();
document.getElementById("displayDate").value = [today.getDate(), today.getMonth()+1, .getFullYear()].join('/');
}

The scripts in the head are executed before the DOM is ready therefore your displayDate doesn't exist.

You have two choices:
- Make your code wait for the DOM to be ready
- Put your script at the bottom of the page (Simpler but uglier)

var d= new Date();
var year= d.getFullYear();
var month= d.getMonth();
var date=d.getDate();
var day= d.getDay();
var h= leftPad(d.getHours(), 2);
var m= leftPad(d.getMinutes(), 2);
var s= leftPad(d.getSeconds(), 2);
var time= (h%12==0?"12":h%12) + ":" + m + ":" + s + (h>11?" PM":" AM");
$("#date-time").html("Today is "+ mos[month] + " " + date + ", " + year + " (" + days[day] +")  + time);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM