1

I'm new to javascript and I have trouble finding a solution that works for what I need. I have a form and when the user submits the form, if a checkbox is checked, it needs to display an alert message.

<form method="post" action="inserting.php" id="form1" class="signupform">
  <input type="hidden" name="allowlang" id="allowlang" value="no" />

  <table cellpadding="4" cellspacing="0" width="100%" border="0" style="text-align:left;">

    <tr>
      <td><input type="checkbox" name="enable" id="enable" value="YES" style="width:15px !important"/>
        &nbsp;
        There has been an issue.</td>
    </tr>

  </table>
<br />

  <table cellpadding="4" cellspacing="0" width="100%" border="0" style="text-align:left;">

    <tr>
      <td height="50"></td>
      <td> <br /><button id="registerButton" type="submit" style="float:left;">Send Data</button></td>
    </tr>

  </table>

</form>

Any help is welcome, thanks in advance.

user2964231
  • 39
  • 1
  • 6
  • http://stackoverflow.com/questions/6515502/javascript-form-submit-confirm-or-cancel-submission-dialog-box –  Nov 07 '13 at 10:33
  • You should edit your post to show some javascript you tried so we can help you on a base you know. check the link added by @Ashish too :) – Asenar Nov 07 '13 at 10:35

4 Answers4

1

Try in jquery

$('#form1').submit(function () {
  if ($('#enable').is(':checked')) {  //if checkbox is checked
    alert("checked");
  }
  else {
    return false;
  } 
})

methods to be learned:

  1. submit()
  2. is()
Praveen
  • 55,303
  • 33
  • 133
  • 164
1

Just change the start of yout form to this

<form method="post" action="inserting.php" id="form1" onsubmit="return validateForm()" class="signupform" name="signupform">

Use this Jscript

function validateForm() {
    if (document.forms["signupform"]["enable"].checked)
        alert("yes, there has been an issue");
}

You do not jQuery for this.

DEMO

MarsOne
  • 2,155
  • 5
  • 29
  • 53
0

Change button type="submit" to button.
Submit the form using submit() function.

$( "#buttonId" ).click(function() {
 //CHECK here for the checkbox;
if(checked) alert();
 $( "#form" ).submit();
});
pratim_b
  • 1,160
  • 10
  • 29
0

I would use an ordinary button that calls a function that shows the alert, and then if necessary, submits the form by calling formObject.submit().

Chong Lip Phang
  • 8,755
  • 5
  • 65
  • 100