我想用javascript计算用户提交的表单数量 然后将提交的表单的数量放在隐藏的输入类型的value属性中,然后使用PHP检索该值(隐藏输入的name属性)
这是我尝试过的一个简单代码,但它不起作用:
form.html
<form method="post" action="submit.php">
<input type="text" name="name" placeholder="name"/>
<input type="text" name="firstname" placeholder="firstname"/>
<input type="hidden" name="nb_submit" value="" id="nb"/>
<input type="submit" value="OK" onclick="count_nb_submit()"/>
</form>
在html表单结束后,我已经输入了javascript代码:
<script type="text/javascript">
var nb=0;
function count_nb_submit()
{
nb ++;
return nb;
document.getElementById('nb').value=nb;
}
</script>
submit.php
<?php
echo $_POST["nb_submit"] ;
?>
答案 0 :(得分:3)
在script
return
之前return
,在你完成职能之前。
如果要停止该功能并返回结果,则只应使用<script type="text/javascript">
var nb=0;
function count_nb_submit() {
nb++;
document.getElementById('nb').value=nb;
return nb;
}
</script>
。返回后的任何内容都不会被阅读。
这就是你的代码应该是这样的:
Category,Date
V,07/01/1993
Y,05/18/2004
X,12/20/2000
X,02/19/2002
X,02/19/2002
X,02/19/2002
V,04/14/2002
V,12/20/2000
V,12/20/2000
V,01/08/2002
Y,07/18/2006
V,12/20/2000
V,12/20/2000
Y,04/20/2006
X,11/12/2009
X,04/03/2014
答案 1 :(得分:0)
在将值设置为字段之前使用function count_nb_submit()
{
// calculate values
document.getElementById('nb').value= //the value you want to assign;
}
,删除返回希望它将起作用。函数应为 -
public Map<String, Object> convert (Map<String, Object> input, String key) {
Map<String, Object> res = new HashMap<>();
for (Entry<String, Object> e : input.entrySet()) {
String newKey = key == "" ? e.getKey() : (key + "." + e.getKey());
if (e.getValue() instanceof Map) {
res.putAll(convert((Map) e.getValue(), newKey)); // recursive call
} else {
res.put(newKey, e.getValue());
}
}
return res;
}
答案 2 :(得分:0)
JS CODE:
<form method="post" name="test" action="submit.php" id="test" target="_blank">
<input type="text" name="name" placeholder="name"/>
<input type="text" name="firstname" placeholder="firstname" onblur="count_nb_submit();"/>
<input type="hidden" name="nb_submit" value="0" id="nb"/>
<input type="button" value="OK" onclick="count_nb_submit();" />
</form>
表格代码:
var x = 0, y = 0;
function start() {
var canvas = document.getElementById("myCanvas"), ctx = canvas.getContext("2d");
animate(ctx);
}
function animate(ctx) {
ctx.clearRect(0,0,500,500);
ctx.fillRect(x, y, 20, 20);
ctx.save();
x++;
window.requestAnimationFrame(animate);
}
答案 3 :(得分:0)
<script type="text/javascript">
var nb = 0;
function count_nb_submit()
{
nb++;
var myform = document.getElementById('test');
document.getElementById("nb").value = nb;
myform.submit();
}
</script>
<form method="post" name="test" action="submit.php" id="test" target="_blank">
<input type="text" name="name" placeholder="name"/>
<input type="text" name="firstname" placeholder="firstname" />
<input type="hidden" name="nb_submit" value="0" id="nb"/>
<input type="button" value="OK" onclick="count_nb_submit();" />
</form>