如何通过JavaScript form.submit()提交提交按钮POST数据

时间:2009-11-18 09:50:21

标签: javascript post submit

假设我有这种形式和脚本:

<SCRIPT LANGUAGE="JavaScript">
function handleSubmit() {   
    return false;  
 }   
function Delete(element) {  
var is_confirmed = confirm("Delete this record? \n" + title + " " + date);  
if (is_confirmed) {         
    document.getElementById("Novinky").submit();
}
}
</SCRIPT> 

<form  action="index.php" method="post" id="Novinky" onsubmit="return handleSubmit();"> 
<input onclick="Delete(this)" value="Smazat" name="delete[12]" type="submit" />
</form> 

有没有办法将提交按钮数据(delete[] -> Smazat)提供给POST请求?

3 个答案:

答案 0 :(得分:2)

您似乎提交的表单与包含“删除”按钮(id=Novinky)的表单不同。在这种情况下,您可以在此表单中添加隐藏字段,并在提交之前将其值设置为“删除”按钮的值:

if (is_confirmed) {                     
    document.getElementById('myhiddenfield').value = element.value;
    document.getElementById('Novinky').submit();
}

更新:

您可以执行以下操作,而不是将单击处理程序附加到删除按钮:

<script type="text/javascript">
function handleSubmit() {   
    return confirm('Delete this record?');  
}   
</script> 

<form  action="index.php" method="post" id="Novinky" onsubmit="return handleSubmit();"> 
    <input value="Smazat" name="delete[12]" type="submit" />
</form>

这将在您提交时自动发布删除按钮的值。


UPDATE2:

<script type="text/javascript">
function handleSubmit() {
    // check which button was clicked:
    alert(window.clicked);
    return confirm('Delete this record?');
}
</script>

<form  action="index.php" method="post" id="Novinky" onsubmit="return handleSubmit();">         
    <input onclick="window.clicked=this.value;" value="Smazat" name="delete[12]" type="submit" />
</form>

答案 1 :(得分:1)

我刚刚发现这段代码可以解决问题:

<SCRIPT LANGUAGE="JavaScript">
function Delete(element) {  
var is_confirmed = confirm("Delete this record? \n" + title + " " + date);  
return is_confirmed;    
}
</SCRIPT> 

<form  action="index.php" method="post" onsubmit="return handleSubmit();"> 
<input onclick="return Delete(this)" value="Smazat" name="delete[12]" type="submit" />
</form>

但是,我仍然对我原来的问题是否有答案感兴趣

答案 2 :(得分:0)

Java脚本

document.cookie = "button="+element.value+";path=/<path of php file>; domain="+window.location.hostname+";";

PHP

<? echo $_COOKIE["button"]; ?>

一定要提供php路径。或者只是设置当前页面的路径。

例如:path = / sample

不要包含php页面。