-2

I am very new in JavaScript and I have the following problem.

I have this form:

<form id="actionButton" action="salwf.do?serv=1" method="post">
    <button id="accept" name="ctrl" value="Accept" type="submit" class="acceptButton" onclick="sottometti(this)">ACCEPT ICON BUTTON</button>
    <button id="cancel" name="ctrl" value="Cancel" type="submit" class="cancelButton" onclick="sottometti(this)">CANCEL ICON BUTTON</button>
    <button id="sap" name="ctrl" value="SAP" type="submit" class="sapButton" onclick="sottometti(this)">SAP ICON BUTTON</button>

    <input id="testId" name="test" type="hidden">
</form>

As you can see this form contains 3 different button. Clicking on one of this button it is performed the sottometti(this) Javascript script, that have the following code:

function sottometti(obj){
    //document.getElementById('testId').value = obj.value;
    document.getElementById('testId').value = obj.value[id]
    document.getElementById('actionButton').submit()
}

This script should submit the previous form (the id of the clicked button) but it don't work. Why?

I think that it is trying to retrieve an actionButton that is not present in my form code.

Can you help me to make it work?

Sparky
  • 98,165
  • 25
  • 199
  • 285
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • Do you want the value of the clicked button or the id to be put into the hidden field? – Jamiec Dec 01 '14 at 17:05
  • Why are you [still](http://stackoverflow.com/questions/27231254/how-can-i-implement-a-javascript-that-submit-this-form-according-to-the-clicket) messing about with JavaScript? Just give the buttons the name `test` instead of copying their values to a hidden input. – Quentin Dec 01 '14 at 17:05
  • `obj.value[id]` is wrong (what does `id` even refer to here?), `obj.value` is correct. However, I believe your form will be submitted twice, since you submit it in code *and* the buttons are submit buttons. – Felix Kling Dec 01 '14 at 17:05

2 Answers2

1

replace obj.value[id] with obj.id.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
Jitesh
  • 232
  • 2
  • 9
  • Although this'll stop any javascript errors, im not sure the OP didnt want the `value` rather than the `id` – Jamiec Dec 01 '14 at 17:06
0

Try this alternative:

<form id="actionButton" action="salwf.do?serv=1" method="post">
    <button name="ctrl" value="accept" type="submit" class="acceptButton">ACCEPT ICON BUTTON</button>
    <button name="ctrl" value="cancel" type="submit" class="cancelButton">CANCEL ICON BUTTON</button>
    <button name="ctrl" value="sap" type="submit" class="sapButton">SAP ICON BUTTON</button>
</form>

I have basically just removed all the JavaScript and the hidden input.

Now, on the server side, test it by simply outputting the value of the ctrl POST variable.

You should clearly see accept, cancel or sap according to the button that the user clicked. No JavaScript required, it's built-in, default functionality :)

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Why are you assuming PHP? Judging by the file extension you'd be safe assuming [java](http://stackoverflow.com/questions/5973505/which-programming-language-has-extension-do) – Jamiec Dec 01 '14 at 17:07
  • @Jamiec Good point. I wasn't paying that close attention. The point still stands, however: only the button you clicked will have its value sent to the server. – Niet the Dark Absol Dec 01 '14 at 17:08
  • Yeah, agreed. Its just the whole need for PHP in an answer nothing to do with PHP is a bit hard to swallow (Much better post-edit) – Jamiec Dec 01 '14 at 17:09
  • @Jamiec The lack of a server-side tag didn't help me ;) But I've edited my answer to be more agnostic. – Niet the Dark Absol Dec 01 '14 at 17:09