1

I have a submit button in my form, I'm not using onsubmit event because i'm gonna add more submits button to this same form. So i'm doing like this:

<script type="text/javascript" src="../script/script.js"></script>

<form id="form_cad" action="my_php.php" method="post" enctype="multipart/form-data">
    <input type="submit" id="submit1" value="Add" onclick="return confirmMessage();"/>
</form>

function confirmMessage()
{
    var x = confirm("...");
    if(x)
    {
        document.getElementById("my_post_value").value = "val1";
        return true;
    }
    return false;
}

I'm using the latest version of Firefox, IE and Google Chrome, but in Chrome the onclick event is not working.

heliosk
  • 1,113
  • 3
  • 23
  • 45
  • 1
    Can you add more context for the script? Specifically, where it is included in comparison to the input? – tcigrand Jun 12 '15 at 18:37
  • 2
    possible duplicate of [onclick button input not working in Chrome](http://stackoverflow.com/questions/19372463/onclick-button-input-not-working-in-chrome) – Mouse Reeve Jun 12 '15 at 18:39
  • 1
    Works fine on my Chrome - what's the error message? – Mackan Jun 12 '15 at 18:50

4 Answers4

0

Instead of using onclick attribute for your submit button, onsubmit for your <form>

Try replacing your code as follows

<form id="form_cad" action="my_php.php" method="post" enctype="multipart/form-data" onsubmit="return confirmMessage();">
    <input type="text" name="email" id="email">
    <input type="submit" id="submit1" value="Add"/>
</form>
Lal
  • 14,726
  • 4
  • 45
  • 70
0

It's working fine in my Chrome, and I've never had problems using events on submit buttons, but since you're using an external js file you might want to try it with a pure js solution, instead of inline:

See example below:

function confirmMessage() {
  var x = confirm("Click OK to submit form");
  if (x) {
    return true;
  }
  return false;
}

document.getElementById('submit1').onclick = function() {
  return confirmMessage();
};
<form id="form_cad" action="my_php.php" method="post" enctype="multipart/form-data" onsubmit="alert('submitting')">
  <input type="submit" id="submit1" value="Add" />
</form>

The alert in the form's onsubmit is just to illustrate that the event is actually only called when confirm() returns true.

Mackan
  • 6,200
  • 2
  • 25
  • 45
0

Because you are in into a form, the onsubmit event is triggered automatically, one way of stop it is after call confirmMessage(), return false. That stop the propagation of events:

<form id="form_cad" action="my_php.php" method="post"   
    enctype="multipart/form-data" onclick="confirmMessage(); return 
    false;">
    <input type="text" name="email" id="email">
    <input type="submit" id="submit1" value="Add"/>
</form>
didando8a
  • 130
  • 1
  • 7
  • The onsubmit event should only be triggered if `confirmMessage` returns true, and that's probably by choice. – Mackan Jun 12 '15 at 19:20
0

I had a similar problem but I fixed it launching the submit event inside the 'onclick' function. I also prevented the event to prevent launching it twice in the case of using Firefox:

submit_button.click(function(event){
    event.preventDefault();
    var selected_file = $("#id-file-to-upload").val();
    if (selected_file) {
        submit_button.prop('disabled', true);
        submit_button.prop('value', "Loading");
    }
    var upload_form = $("#id-form-to-submit");
    upload_form.submit();
});
bufias
  • 1