Php 添加一个javascript变量并随post一起发送
我有这个密码:Php 添加一个javascript变量并随post一起发送,php,javascript,html,Php,Javascript,Html,我有这个密码: $image_action = $_POST['image_action']; echo ' <form action="" method=post onmousedown="rightbutton(event)"> <input type="image" src="image.jpeg" style="cursor:crosshair" /> </form&g
$image_action = $_POST['image_action'];
echo '
<form action="" method=post onmousedown="rightbutton(event)">
<input type="image"
src="image.jpeg"
style="cursor:crosshair" />
</form> ';
<script type="text/javascript">
function rightbutton(event)
{
if(event.button==2) { document.getElementById("click").innerHTML="Last action: Right Click"; }
else{
if(event.button==0) { document.getElementById("click").innerHTML="Last action: Left Click"; }
else {
if(event.button==1) { document.getElementById("click").innerHTML="Last action: Middle Click"; }
}
}
}
</script>
但是我得到了一个错误:
未定义索引:key
如果通过console.log()函数调试隐藏元素会发生什么?除了Samuel的评论之外,我还建议将值设置为event.button值,而不仅仅是event,它不能通过post变量传递……嗯,我有“form.setAttribute(“method”,method”);”方法后=方法| |“post”;而且没有任何变化:(我还更改了“hiddenField.setAttribute(“value”,event.button)”
method = method || "post";
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", image_action);
hiddenField.setAttribute("value", event);
form.appendChild(hiddenField);
document.body.appendChild(form);
form.submit();