1

I have a form returned back from a PHP request. The form has a submit button and a regular button. The submit seems to work just fine, but the regular one won't work! Please advise.

Here is my HTML:

<tr id='actionRow' name='actionRow' hidden='hidden'>
    <td>
        <input class='actionBtn' type='submit' id='confirm' name='confirm' 
            value='Confirm' onclick='genChanged();'/> 
    </td>
    <td>                                
        <input class='actionBtn' type='button' id='cancel' name='cancel' 
            value='Cancel' onclick='cancel();' /> 
    </td>
</tr> 

And here is the cancel() function:

function cancel(){
        alert('in cancel');
        document.getElementById('editRow').hidden = false;
        document.getElementById('actionRow').hidden = true;
        window.location.replace("admin.php");
    };

The alert never appears!

Is it even right to put multiple non-submit buttons in one form?

UPDATE my form looks something like:

<form action='save.php' method='POST' id='myForm'>

I've added the line document.getElementById('myForm').submit(); to the getChange(); function, to make the button be:

<input class='actionBtn' type='button' id='confirm' name='confirm' 
       value='Confirm' onclick='genChanged();'/>

Yet, cancel(); function still doesn't work!

Roshdy
  • 1,703
  • 3
  • 25
  • 34

1 Answers1

1

You can't use the same name for the id and the function name.

In my sample 'a' have the same id and function name test() and b have different id and function name cancel()... cancel work fine and test don't.

<script>
    function cancel(){
        alert('in cancel');
    };
    function test(){
        alert("in test")
    };
</script>

<body>
  <form action='save.php' method='POST' id='myForm'>
    <tr id='actionRow' name='actionRow' hidden='hidden'>
      <td>
        <input class='actionBtn' type='button' id='test' name='a' 
           value='a' onclick='test();'/>
      </td>
      <td>                                
        <input class='actionBtn' type='button' id='b' name='b' 
          value='b' onclick='cancel()' /> 
      </td>
   </tr> 
 </form>
</body>

For more informations you can see this helpful answer : https://stackoverflow.com/a/9160009/1318727

Community
  • 1
  • 1