简体   繁体   English

POSTDATA没有使用javascript submit()函数传递

[英]POSTDATA not being passed using javascript submit() function

I'm trying to write a form that will update the hidden field values and then submit, after which the values will be input into the mysql database. 我正在尝试编写一个表单来更新隐藏的字段值然后提交,之后将值输入到mysql数据库中。 However, while the form does seem to submit, the $_POST array seems to be empty and i get "unidentified index" errors whenever i try to access any of the $_POST elements?? 但是,虽然表单确实提交,$ _POST数组似乎是空的,每当我尝试访问任何$ _POST元素时,我会收到“未识别的索引”错误?

The relevant code is as follows: 相关代码如下:

<?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";
}
?>

the JS function to fill the hidden fields and submit the form: 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>

and the form: 和形式:

<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>

Any help would be appreciated, thanks! 任何帮助将不胜感激,谢谢!

You shouldn't use location in JavaScript; 你不应该在JavaScript中使用location ; if you do something like 如果你做的事情

location='http://example.com'

you're actually redirecting the page to example.com. 你实际上是将页面重定向到example.com。

You should remove the line 你应该删除该行

location = "http://127.0.0.1/experiment/index.php?a=true";

and change it to something like: 并将其更改为:

document.getElementById('testform').action="http://127.0.0.1/experiment/index.php?a=true";

When you submit your form, you go to index.php , not index.php?a=true - try doing 当你提交表单时,你去index.php ,而不是index.php?a=true - 尝试做

location = "http://127.0.0.1/experiment/index.php?a=true";
document.getElementById('testform').action = location;

Replace 更换

<input type = "button" name = 'update' id = 'update' value = "Update" onClick = 'dosmt(this.form)'>

with

<input type = "button" name = 'update' id = 'update' value = "Update" onClick = 'dosmt(document.testform)'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM