最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to get submit button POST Data while submitting by JavaScript form.submit() - Stack Overflow

programmeradmin0浏览0评论

Let's say I have this form and script:

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

Is there a way to get the submit button data (delete[] -> Smazat) to the POST request?

Let's say I have this form and script:

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

Is there a way to get the submit button data (delete[] -> Smazat) to the POST request?

Share Improve this question edited Nov 18, 2009 at 10:58 Jakub Stejskal asked Nov 18, 2009 at 9:50 Jakub StejskalJakub Stejskal 6132 gold badges10 silver badges25 bronze badges 2
  • make your <input type="button"> so that the onclick will work – Treby Commented Nov 18, 2009 at 10:07
  • it works anyway.. I chose submit button so it would work with JavaScript disabled – Jakub Stejskal Commented Nov 18, 2009 at 15:25
Add a ment  | 

3 Answers 3

Reset to default 2

It seems that you are submitting a different form than the one that contains the Delete button (id=Novinky). In this case you could add a hidden field in this form and set it's value to the value of the Delete button just before submitting it:

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

UPDATE:

Instead of attaching a click handler to the delete button you could do this:

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

This will automatically post the value of the Delete button when you submit it.


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>

I've just found out that this code does the trick:

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

However, I'm still interested whether there is an answer to my original question

Java Script

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

PHP

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

be sure to supply the php path. or just set the path of the current page.

e.g: path=/sample

dont include the php page.

发布评论

评论列表(0)

  1. 暂无评论