1

我正在尝试编写一个将更新隐藏字段值然后提交的表单,之后这些值将输入到 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>

任何帮助将不胜感激,谢谢!

4

3 回答 3

2

你不应该location在 JavaScript 中使用;如果你做类似的事情

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";
于 2012-05-28T13:04:31.590 回答
1

当您提交表单时,您会去index.php,而不是index.php?a=true- 尝试做

location = "http://127.0.0.1/experiment/index.php?a=true";
document.getElementById('testform').action = location;
于 2012-05-28T11:41:37.840 回答
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)'>
于 2012-05-28T11:31:08.220 回答