-1

I want to submit a form only when a check box is checked, or display "Please check the check box", is there any way to do that? Can any one guide me ?

thanks

<form  name="myForm" action="mailsent.php" method="post">

<input type="checkbox" class="chk_row" name="chk1[]"" value=" '.$rows["id"].'"/>

<input type="submit" value=""  style="margin:7px 26px -27px 1424px;background-image: url(/image/exporttt.png);background-repeat: no-repeat;cursor:pointer;" >

</form>

Edited code:

<form  name="myForm" action="mailsent.php" method="post">

    <input type="checkbox" class="chk_row" id="chk1" name="chk1[]"" value=" '.$rows["id"].'"/>

    <input type="submit" value=""  style="margin:7px 26px -27px 1424px;background-image: url(/image/exporttt.png);background-repeat: no-repeat;cursor:pointer;" >

    </form>

<script>

$('form').submit(function(){
  if(!$('#chk1').is(':checked')){
      alert("Please Check ");
      return false;
  }
});
</script>
Ekramul Hoque
  • 4,957
  • 3
  • 30
  • 31
arok
  • 1,795
  • 7
  • 27
  • 56

4 Answers4

1

Using JQuery
You should give your checkbox unique ID

$('form').submit(function(){
 var flag=0;
  $('.chk_row').each(function(){
     if(($(this).is(':checked'))){
      flag=1
      return false;
     }
   });
  if(flag==0){
    alert("Please Check Checkbox");
    return false
  }
});

Your your_checkboxId is what you give id in your input chekcbox eg.

<input type="checkbox" id="your_checkboxId" name="chk_name" value="some" / >
Ekramul Hoque
  • 4,957
  • 3
  • 30
  • 31
0

You can validate using jquery, give your submit button with id 'submit', and then use this code

$('#submit').click(function(e){
   e.preventDefault();
   if( $("[name='chk1[]']").is( ':checked')){
      return true;
   } else {
     // your custom code here
      return false;
    }
})
Saiqul Haq
  • 2,287
  • 16
  • 18
0

try this

$("#submit").click(function(e)
    {
    if($("#check").is(':checked'))
    {
        $("#form").prop("action","mailsent.php");
    }else
    {
    e.preventDefault();
    }
    });

And you need not specify the action in form tag. #form is id of form, #check is id of checkbox.

Deepu Sasidharan
  • 5,193
  • 10
  • 40
  • 97
0

assuming there is only one checkbox here, and I am selecting it using the class name. You may modify the selector as per the implementation to pick the desired one.

$(document).ready(function(){
$('.chk_row').change(function(e){
if($(this).attr('checked') === 'checked')
{
$('form[name="myForm"]').submit();
}
});
});
Harsh Chiki
  • 79
  • 1
  • 10