Liza Liza - 1 month ago 18
HTML Question

How to insert the value of javascript into html

I want to get the javascript value into my html input value and insert it into the database after the user click the submit

<script>
var currentDate = new Date();

if (currentDate.getDay() == 5) {
var numberOfDaysToAdd = 4; //Adding 4 to skip sat. & sun. if Friday
} else {
var numberOfDaysToAdd = 2; //Adding 2 days if not Friday
}

currentDate.setDate(currentDate.getDate() + numberOfDaysToAdd);
var dd = currentDate.getDate();
var mm = currentDate.getMonth() + 1;
var y = currentDate.getFullYear();
var someFormattedDate = y + '-' + mm + '-' + dd;

document.getElementById("display").innerHTML = someFormattedDate;

</script>

<?php

echo $_GET['someFormattedDate'];

?>
<input type="text" class="w8em format-y-m-d highlight-days-67 range-low-today" name="due_date" id="sd" maxlength="10" value="<?php echo " someFormattedDate " style="border: 3px double #CCCCCC; " disabled/>

Onx Onx
Answer Source

Since your input id is sd,

 document.getElementById("sd").value = someFormattedDate;

Note: Elements with Disabled attribute are not submitted, enable it before submit.

Full Code:

<?php
    echo $_GET['due_date'];
    ?>

    <form name="myform" id="myform" method="GET">
    <input type="text" class="w8em format-y-m-d highlight-days-67 range-low-today" name="due_date" id="sd" maxlength="10" style="border: 3px double #CCCCCC; " readonly/>
    <input type="submit" name="btn_submit" value="Submit">
</form>

<script>
    var currentDate = new Date();
    if (currentDate.getDay() == 5) {
        var numberOfDaysToAdd = 4; //Adding 4 to skip sat. & sun. if Friday
    } else {
        var numberOfDaysToAdd = 2; //Adding 2 days if not Friday
    }
    currentDate.setDate(currentDate.getDate() + numberOfDaysToAdd);
    var dd = currentDate.getDate();
    var mm = currentDate.getMonth() + 1;
    var y = currentDate.getFullYear();
    var someFormattedDate = y + '-' + mm + '-' + dd;

     document.getElementById("sd").value = someFormattedDate;

</script>