簡體   English   中英

POSTDATA沒有使用javascript submit()函數傳遞

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

我正在嘗試編寫一個表單來更新隱藏的字段值然后提交,之后將值輸入到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";

當你提交表單時,你去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)'>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM