我正在尝试编写一个表单,该表单将更新隐藏字段值然后提交,之后这些值将被输入到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>
任何帮助将不胜感激,谢谢!
你不应该在 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";
- 阅读有关
location
变量的更多信息
当您提交表单时,您会转到index.php
,而不是index.php?a=true
- 尝试这样做
location = "http://127.0.0.1/experiment/index.php?a=true";
document.getElementById('testform').action = location;
替换
<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)'>