views:

1693

answers:

3

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?

+2  A: 

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>
Darin Dimitrov
My bad... I've accidentally deleted form's id while simplifying the example ;) I'd like to avoid using any additional hidden fields... I was wondering if I could somehow modify functionality of submit() or set the button to be POSTed...
Corvus
Please see my UPDATE.
Darin Dimitrov
This works as long as long as there aren't any different submit buttons in the form (eg. edit, save etc.).. which is not my case (sorry, I should have made that clear... I guess I've oversimplified that example a bit :) )
Corvus
Even if you have other submit buttons, only the value of the button you clicked will be posted which will allow you to determine it in your server side script.
Darin Dimitrov
I was pointing out the fact that you'd still get the confirmation dialog... I can't think up any way to let handleSubmit() know which button was clicked without using click handlers and hidden fields again... any ideas?
Corvus
Check out my UPDATE2 :-)
Darin Dimitrov
Nice, I guess that's an answer :-) Thank you very much ;-)Just one more question.. what did you mean by the "el" parameter of handleSubmit()?
Corvus
It was a typo, I've removed it :-)
Darin Dimitrov
+1  A: 

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

Corvus
A: 

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.

Treby
Sorry, but I can't see how alert() has anything to do with what is sent in POST request.. : /
Corvus
That's a interesting solution, but my point was to make the script act the same way as the form would with JavaScript disabled, so the form would work both with and without JavaScript and it wouldn't have to be processed separately on the server side... hence the POST request
Corvus