2017-04-24 30 views
2

所以即时尝试是将POST输入添加到Onclick function上的窗体。所以,当我通过Javascript检查信息是否可以时,在通过Javascript form.submit()进行操作之前,我想添加更多信息,例如IdCrono。 什么IM,现在做的是在Form.action = "myPhpFunction.php"通过动作添加GET变量所以这将是如何通过javascript为表单添加值?

"myPhpFunction.php?key=1&IdCrono=1" 

反正加密钥和IdCrono通过JavaScript中的POST?

我的代码如下所示:

function GuardarMilestone(a) { 
 
    var id = a.id; 
 
    var idCrono = Math.floor(a.id/10); 
 
    alert(idCrono); 
 
    var form = document.getElementById("Form" + id); 
 
    if (id % 10 == 0) { 
 
    //alert("Resto 0"); 
 
    var date = form.elements["FechaInicio"]; 
 
    } else { 
 
    //alert("Resto 5"); 
 
    var date = form.elements["FechaFin"]; 
 
    } 
 
    if (date.value != "") { 
 
    form.action = "InteriorInternalFunciones.php?key=1&id=" + id; 
 
    form.target = "_self"; 
 
    form.submit(); 
 
    } else { 
 
    alert("Please fill in the date before saving"); 
 
    } 
 
}
<form method="POST" id="Form<?php echo $rstMilestones->fields['IdCronograma']; ?>0" action=""> 
 
    <input type="date" style="font-size: 9px; font-style: bold;" name="FechaInicio" value="<?php echo $strFaseInicioSinFormato;?>"> 
 
    <input type="hidden" name="IdCronograma" value="<?php echo $rstMilestones->fields['IdCronograma']; ?>"> 
 
    <a href="#botones" id="<?php echo $rstMilestones->fields['IdCronograma']; ?>0" onclick="GuardarMilestone(this)"><img src="images/PC_si.png" alt="Save"></a> 
 
    <a href="#botones" id="<?php echo $rstMilestones->fields['IdCronograma']; ?>0" onclick="EditarFechaCronograma(this)"><img src="images/PC_no.png" alt="cancel"></a> 
 
</form>

+2

为'key,id'添加一个空值的两个隐藏输入,并给它们一个id(html属性)。然后在提交之前,您可以在提交表单之前更新隐藏的输入。 'document.getElementById(“IdCrono”)。value = id;' – JustOnUnderMillions

+1

你的表单有'POST',你正在创建'GET'就是故意的吗? – RST

+0

@RST是它的故意,因为我不知道我是否可以动态添加POST变量来发送,我不喜欢使用GET变量。这就是为什么我问是否有办法做到这一点。 –

回答

1

也许这将帮助,只需添加此功能代码:

function addFields(form, inputName, inputValue) { 
    // Create an <input> element 
    var input = document.createElement("input"); 
    input.type = "hidden"; 
    input.name = inputName; 
    input.value = inputValue; 
    form.appendChild(input); 
} 

和替换这一行:

form.action = "InteriorInternalFunciones.php?key=1&id=" + id; 

与此:

form.action = "InteriorInternalFunciones.php; 
addFields(form, "key", 1); 
addFields(form, "id", id); 

这将增加隐藏字段到你的代码提交表单之前,一切将与POST方法来发送。