working on date change on button click using javascript
Clash Royale CLAN TAG#URR8PPP
working on date change on button click using javascript
I am working with change date on button click using javascript.
My problem is that when the page loads, it shows today's date and when I click on a button it changes a the date and sends the value through url. Up to this point everything is working fine but when the page gets reloaded it shows today's date again. Instead of this it should show changed date.
I'm working on this for the last 2 days and I did not get any proper solution.
Here is my js code:
document.getElementById("up").onclick = function()
var i = dataI.valueOf() + 86400000 ;
dataI = new Date(i);
document.getElementById("dateD").innerHTML = dataI.toDateString();
var b = dataI.toDateString();
window.location="datechange.php?date=" + b;
document.getElementById("down").onclick = function()
var i = dataI.valueOf() - 86400000 ;
dataI = new Date(i);
document.getElementById("dateD").innerHTML = dataI.toDateString();
var c = dataI.toDateString();
window.location.href ="datechange.php?previous=" + c;
var dataI = new Date();
document.getElementById("dateD").innerHTML = dataI.toDateString();
Here is my body:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<input type=button id=down value=down >
<span id=dateD></span>
<input type=button id=up value=up >
<div id="div1-wrapper">
<div id="abcd">
<?php
$date2 = $_GET['date'];
echo $date2;
$date3 = $_GET['previous'];
echo $date3;
?>
</div>
</div>
2 Answers
2
I think you can do it by storing the date in session like below, I have made changes in your code, see if it solve your problem ....
<?php
session_start();
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<input type="button" id="down" value="down" >
<?php
$date = '';
if(isset($_GET['date'])) $date = $_GET['date']; ;
if(isset($_GET['previous'])) $date = $_GET['previous']; ;
if(isset($date))
$_SESSION['dt']=$date;
?>
<span id=dateD><?php echo $_SESSION['dt'];?></span>
<input type="button" id="up" value="up" >
<div id="div1-wrapper">
<div id="abcd">
<?php
echo $date;
?>
</div></div>
<script language="javascript">
document.getElementById("up").onclick = function()
var i = new Date(document.getElementById("dateD").innerHTML).valueOf() + 86400000 ;
dataI = new Date( i);
document.getElementById("dateD").innerHTML = dataI.toDateString();
var b = dataI.toDateString();
window.location="file.php?date="+b;
document.getElementById("down").onclick = function()
var i = new Date(document.getElementById("dateD").innerHTML).valueOf() - 86400000 ;
dataI = new Date( i);
document.getElementById("dateD").innerHTML =dataI.toDateString();
var c = dataI.toDateString();
window.location.href ="file.php?previous="+c;
var dataI = new Date();
if(document.getElementById("dateD").innerHTML == '')
document.getElementById("dateD").innerHTML = dataI.toDateString();
if(document.getElementById("dateD").innerHTML == dataI.toDateString())
document.getElementById("down").style.display='none';
</script>
have a look at updated answer
– shubhangee
Aug 8 at 7:20
hey shubhangee is it possible to disable previous button if date is eqaul to todays date?
– jonathan harry
Aug 8 at 7:31
I have updated a code for hiding a button, have a look on updated code.
– shubhangee
Aug 8 at 7:36
you are awesome shubhangee you solved my problem thanks alot
– jonathan harry
Aug 8 at 7:38
Here is script in jquery that solve your problem.
var dataI = new Date();
var current_tm = dataI.valueOf();
$(function()
var urlParams = new URLSearchParams(window.location.search);
var display_date;
if(urlParams.has('date'))
var tmp_stamp = urlParams.get('date');
display_date = new Date(parseInt(tmp_stamp)).toDateString();
else
display_date = dataI.toDateString();
$("#dateD").html(display_date);
$("#up").click(function()
if(urlParams.has('date'))
var i = parseInt(urlParams.get('date'))+ 86400000 ;
else
var i = current_tm + 86400000 ;
var b = new Date(i);
$("#dateD").innerHTML = b.toDateString();
window.location="datechange.php?date="+ i;
);
$("#down").click(function()
if(urlParams.has('date'))
var i = parseInt(urlParams.get('date')) - 86400000 ;
else
var i = current_tm - 86400000 ;
var c = new Date(i);
$("#dateD").innerHTML = c.toDateString();
window.location="datechange.php?date="+ i;
);
);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
its working but now problem is when i again click button its not changing date
– jonathan harry
Aug 8 at 7:17