我正在尝试编写一个更新隐藏字段值然后提交的表单,之后将值输入到mysql数据库中。但是,虽然表单确实提交,$ _POST数组似乎是空的,每当我尝试访问任何$ _POST元素时,我都会收到“未识别的索引”错误?
相关代码如下:
<?php
if(isset($_GET['a'])){
$title = $_POST['left'];
$sql = "UPDATE boxes SET topx = '".$_POST['left']."', topy = '".$_POST['top']."', width = '".$_POST['width']."', height = '".$_POST['height']."' WHERE id = '2'";
// this is where I get "unidentified index" errors
mysql_query($sql);
}
else {
$title = "test";
}
?>
JS函数填充隐藏字段并提交表单:
<script type = "text/javascript">
function dosmt(form){
JStopx = dd.elements.field2.x; alert(JStopx);
JStopy = dd.elements.field2.y; alert(JStopy);
JSwidth = dd.elements.field2.w; alert(JSwidth);
JSheight = dd.elements.field2.h; alert(JSheight);
alert("test2");
alert(document.getElementById('top').value);
document.getElementById('top').value = JStopy; alert("test1");
document.getElementById('left').value = JStopx;
document.getElementById('width').value = JSwidth;
document.getElementById('height').value = JSheight;
alert("waah");
location = "http://127.0.0.1/experiment/index.php?a=true";
alert ("OK");
document.getElementById('testform').submit();
}
</script>
和表格:
<form name = 'testform' method = "post" id = 'testform' action = "index.php">
<input type = "hidden" name = 'top' id = 'top' value = ''/>
<input type = "hidden" name = 'left' id = 'left' value = ''/>
<input type = "hidden" name = 'width' id = 'width' value = ''/>
<input type = "hidden" name = 'height' id = 'height' value = ''/>
<input type = "hidden" name = 'placeholder' id = 'placeholder' value = 'blah'/>
<input type = "button" name = 'update' id = 'update' value = "Update" onClick = 'dosmt(this.form)'>
</form>
任何帮助将不胜感激,谢谢!
答案 0 :(得分:2)
您不应在JavaScript中使用location
;如果你做了像
location='http://example.com'
您实际上是将页面重定向到example.com。
您应该删除该行
location = "http://127.0.0.1/experiment/index.php?a=true";
并将其更改为:
document.getElementById('testform').action="http://127.0.0.1/experiment/index.php?a=true";
答案 1 :(得分:1)
当您提交表单时,请转到index.php
,而不是index.php?a=true
- 尝试
location = "http://127.0.0.1/experiment/index.php?a=true";
document.getElementById('testform').action = location;
答案 2 :(得分:0)
替换
<input type = "button" name = 'update' id = 'update' value = "Update" onClick = 'dosmt(this.form)'>
与
<input type = "button" name = 'update' id = 'update' value = "Update" onClick = 'dosmt(document.testform)'>