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?
- 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
3 Answers
Reset to default 2It 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.