I'm a beginner and I would appreciate your help. I'm using two jquery datepickers to get the start and end date for a week. and this is my code for that:
Html and Php:
<form method="post" action="">
<div id="rangeDate">
<input placeholder="Start Date" name="sDate" type="text" class="dateInput" id="startDate">
<input placeholder="End Date" name="eDate" type="text" class="dateInput" id="endDate">
<input type="submit" value="Submit" name="submitBtn"><br>
</div>
</form>
<?php
include("C:/wamp/www/sunstar/DB_Connection.php");
if(isset($_POST['submitBtn']))
{
$date1 = $_POST['sDate'];
$date2 = $_POST['eDate'];
if($date1 == ""){
echo "<script>alert('Enter The Start Date!')</script>";
exit();
}else{
function returnDates($fromdate, $todate){
$fromdate = \DateTime::createFromFormat('m/d/Y', $fromdate);
$todate = \DateTime::createFromFormat('m/d/Y', $todate);
return new \DatePeriod(
$fromdate,
new \DateInterval('P1D'),
$todate->modify('+1 day')
);
}
$datePeriod = returnDates($date1, $date2);
foreach($datePeriod as $date) {
echo "<form method='post' action=''>";
echo "<input id='specdates' name='7daysdates' type='text' value='" . $date->format('m/d/Y'), PHP_EOL . "' disabled><br>";
echo "</form>";
}
$insert_date = "INSERT INTO tbldtr (DateFrom, DateTo) VALUES ('$date1', '$date2')";
if(mysqli_query($dbcon, $insert_date)){
echo "<script>alert('You have successfully inserted the date!')</script>";
}
}/*END of Else Condition*/
}
?>
<script>
var dateToday = new Date();
$(function(){
$( "#startDate" ).datepicker({
numberOfMonths: 1,
showButtonPanel: true,
maxDate: dateToday,
});
$('#endDate').datepicker({maxDate: dateToday});
$('#startDate').change(function() {
var date2 = $('#startDate').datepicker('getDate', '+1d');
date2.setDate(date2.getDate()+6);
$('#endDate').datepicker('setDate', date2);
});
});
</script>
The foreach($dateperiod as $date) loop could display the days in textboxes but I can't save them separately into the database because I only have 1 input text and it loops 7 times. What I want to achieve is to display them into 7 separate input text so I could save them into the database as FirstDay, SecondDay, ThirdDay, FourthDay, FifthDay, SixthDay and SeventhDay.
My purpose of saving them separately is because when I retrieve them the output should look like this: