So I have this function that creates a number of textareas depending on what month it is.
For example:
If it's 'February', then 28 'textareas' are created. If it's 'March', then 31 'textareas' are created and so on. When the user clicks on the 'textareas' and then the forms button, the value of the textareas inserts into the MySQL database.
My problem right now is that the value that goes into the database is not the value of the textareas, e.g. 2018-02-19, it is always 2018-02-28.
function daysInMonth(month, year) {
var days;
switch (month) {
case 1: // Feb, our problem child
var leapYear = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
days = leapYear ? 29 : 28;
break;
case 3:
case 5:
case 8:
case 10:
days = 30;
break;
default:
days = 31;
}
return days;
}
var showDate = new Date();
var months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var weeks = ["Sunday", "Monday", "Tuseday", "Wednesday", "Thursday", "Friday", "Saturday"];
function drawTable(forDate) {
var daysInMonth = new Date(forDate.getFullYear(), forDate.getMonth() + 1, 0).getDate();
var cellsToDraw = daysInMonth;
var newdate = forDate.getFullYear() + "-" + ("0" + (forDate.getMonth() + 1)).slice(-2);
var table = document.getElementById("table");
table.innerHTML = "";
for (var r = 0; r < (daysInMonth / 7); r++) {
var newRow = document.createElement("tr");
table.appendChild(newRow);
for (var c = 0; c < 31 && cellsToDraw > 0; c++) {
var day1 = ("0" + (c + 1)).slice(-2);
var textarea = document.createElement("textarea");
textarea.setAttribute("type", "button");
textarea.setAttribute("name", "day");
textarea.setAttribute("value", newdate + "-" + day1);
textarea.setAttribute("placeholder", day1);
newRow.appendChild(textarea);
textarea.setAttribute("name", "day");
textarea.setAttribute("day", newdate + "-" + day1)
textarea.innerHTML = newdate + "-" + day1;
cellsToDraw--;
}
}
}
window.onload = function() {
document.getElementById("displayingMonth").innerHTML = months[showDate.getMonth()];
drawTable(showDate);
};
<form class="" action="insert.php" method="post">
<table id="table">
<input id="day"type="hidden" name="day" value="">
<input id="btn" type="submit" name="" value="Press">
</form>
php
$day = mysqli_real_escape_string($conn, $_REQUEST['day']);
$stmt = "INSERT INTO table (day) VALUES('$day')";
if(empty($day) ){
$_SESSION['error'] = "Error";
header('Location: insert.php', true, 303);
exit();
} else {
if (mysqli_query($conn, $stmt)) {
header('Location: insert.php', true, 303);
exit;
}else {
$error= "Error: " .mysqli_error($conn);
echo "$error";
}
}