I have index.php
and add.php
. My question is when I click on a link of day in index.php
, then go to add.php
and store in input date the value of clicked day
index.php
<td><a href ="add.php"><?php $date1=date('y-m-d');echo $date1;?></a></td>
<td><a href ="add.php"><?php $date2=date('y-m-d' ,strtotime("+1day"));echo $date2;?></a></td>
add.php
<input type="date" name"date" >
I need that when click on date ,auto store in input without filling
You can pass the the strtotime("+1 day")
to the $_GET
parameter, and extract it and pass it onto the value on your second page. Something like this for your index.php
<td><a href="add.php?date=<?php echo time(); ?>"><?php echo date('y-m-d'); ?></a></td>
<td><a href="add.php?date=<?php echo strtotime("+1 day"); ?>"><?php echo date('y-m-d', strtotime("+1 day")); ?></a></td>
And in add.php
you can fetch that value with $_GET['date']
, which you can convert to a date with date("Y-m-d", $_GET['date'])
, which you add in as a value to the input.
I've added the time into a ternary operator, so that you avoid getting Undefined index warnings. By default, it's set to be the date of today.
<input type="date" name="date" value="<?php echo (!empty($_GET['date']) ? date("Y-m-d", $_GET['date']) : date("Y-m-d")); ?>">
In index.php
<a href="add.php?day=tuesday">Tuesday</a>
<a href="add.php?day=wednesday">Wednesday</a>
then, in add.php
<?php $day = $_GET['day']; ?>
index.php
<td><a href ="add.php?date=<?php $date1=date('y-m-d');echo $date1;?>"><?php echo $date1;?></a></td>
<td><a href ="add.php?date="<?php $date2=date('y-m-d' ,strtotime("+1day"));echo $date2;?>><?php echo $date2;</a></td>
By clicking on one of above <a>
, it will redirect to add.php
with get parameter date
.
In add.php
, this date varible can be retrived using php and can be stored to input element.
add.php
<input type="date" name="date" value=""<?php echo $_GET['day']; ?>"">
output of <?php echo $_GET['day']; ?>
should be in formate 'yyyy-mm-dd'