Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将javascript的值插入html_Javascript_Php_Jquery_Html - Fatal编程技术网

如何将javascript的值插入html

如何将javascript的值插入html,javascript,php,jquery,html,Javascript,Php,Jquery,Html,我希望在用户单击submit之后,将javascript值转换为html输入值并将其插入数据库 <script> var currentDate = new Date(); if (currentDate.getDay() == 5) { var numberOfDaysToAdd = 4; //Adding 4 to skip sat. & sun. if Friday } else { var numberOfDa

我希望在用户单击submit之后,将javascript值转换为html输入值并将其插入数据库

<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/>

var currentDate=新日期();
if(currentDate.getDay()==5){
var numberOfDaysToAdd=4;//如果星期五,则添加4以跳过星期六和星期日
}否则{
var numberOfDaysToAdd=2;//如果不是星期五,则添加2天
}
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;

首先,将JavaScript代码放在您想要使用的输入之后。这样,当代码执行时,输入就存在于页面上

其次,使用输入的实际
id

第三,设置
.value
属性

第四,将输入放入表单中

第五,不要禁用输入

大概是这样的:

<form>
    <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;" disabled/>
    <input type="submit" value="Submit" />
</form>
<script>
    // The rest of your JavaScript code, formatting your date value

    document.getElementById("sd").value= someFormattedDate;
</script>

//剩下的JavaScript代码,格式化日期值
document.getElementById(“sd”).value=someFormattedDate;
除非另有规定,否则当单击提交按钮时,默认情况下,此
将向当前URL提交
GET
请求

除此之外,我还不太清楚您打算在这里用PHP代码做什么。为什么您试图输出一个尚未提交的值,或者输出一个文本字符串,或者您正在尝试的任何东西。


<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").innerHTML = someFormattedDate;

</script>
var currentDate=新日期(); if(currentDate.getDay()==5){ var numberOfDaysToAdd=4;//如果星期五,则添加4以跳过星期六和星期日 }否则{ var numberOfDaysToAdd=2;//如果不是星期五,则添加2天 } 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”).innerHTML=someFormattedDate;

描述:-您正在使用错误的元素绑定数据。

因为您的输入id是sd

 document.getElementById("sd").value = someFormattedDate;
注意:不提交具有禁用属性的元素,请在提交之前启用它。

完整代码:

<?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>

var currentDate=新日期();
if(currentDate.getDay()==5){
var numberOfDaysToAdd=4;//如果星期五,则添加4以跳过星期六和星期日
}否则{
var numberOfDaysToAdd=2;//如果不是星期五,则添加2天
}
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;

您必须添加表单标签或使用xhr
getElementById(“display”)
应该是
getElementById(“sd”)
而且输入没有
innerHTML
,他们有一个
属性告诉你,它现在工作了,因为我的脚本在表单中,这就是它不工作的原因,但是当我看到你的代码时,脚本在表单之外,现在我得到了值,并显示它一个问题,为什么它是未定义的索引:提交后的到期日尽管此输入类型有一个值,谢谢!这是因为,当表单第一次加载时,表单未提交,您可以检查表单是否已提交,或者只需检查isset函数,例如,感谢您的努力,它现在正在工作:)