如何从textarea获得正确的价值

时间:2018-02-19 18:20:27

标签: javascript php html

所以我有这个功能,根据它的月份创建一些textareas。

例如:

如果是'二月',则会创建28'textareas'。 如果它是'March',则会创建31'textareas',依此类推。 当用户单击“textareas”然后单击“表单”按钮时,textareas的值将插入到MySQL数据库中。

我现在的问题是进入数据库的值不是textareas的值,例如2018-02-19,总是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";



  }
}

1 个答案:

答案 0 :(得分:0)

由于每行需要唯一的ID和BTN,您需要执行以下操作:

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", "bday" + c); // concatenate the number so each is unique
    textarea.setAttribute("value", newdate + "-" + day1);
    textarea.setAttribute("placeholder", day1);
    newRow.appendChild(textarea);
    textarea.setAttribute("name", "day" + c); // same here
    textarea.setAttribute("day", newdate + "-" + day1)
    textarea.innerHTML = newdate + "-" + day1;
    cellsToDraw--;
}

请注意,我也更改了按钮的名称 - 它不应该与隐藏输入名称相同。