Sereen
Sereen

Reputation: 39

Passing checkbox value in ajax

Please, I need help in passing the check box value through ajax to other php file

This is my form :

 <form class="form-horizontal" onsubmit="Javascript:return validate();" method="post" id="form">



 <b>Jawwal Number</b>

<input name="msisdn" type="text" id="msisdn" class="form-control" required="" autofocus="" style="margin-top: 10px;margin-bottom: 10px" >


 <b>Username</b>

<input name="username" type="text" id="username" class="form-control" required="" autofocus="" style="margin-top: 10px;margin-bottom: 10px" >



<b>Add Extra Security (Activation Code)</b>
<input type="checkbox" name="security" id="security">

<input type="submit" value="submit" class="btn btn-primary"> 

  </form>

And this is my Ajax code :

$("#form").submit(function(event) {

  /* stop form from submitting normally */
  event.preventDefault();

  /* set all the vars you want to post on here */
  var parameters = { 
    'msisdn': $('input[name="msisdn"]').val(),
    'username': $('input[name="username"]').val(),

    **'security':$('input[name="security"]').val(),**
    'submit': $('input[name="submit"]').val()
  };

    $.ajax({
         url: '/bulk2/admin/process/add_user.php',
         method:'POST',
         data: parameters,
         success: function(msg) {
            $('#test').append(msg);
         }
    })

});

What should I do so that I can pass the checkbox value to the other page ?

Upvotes: 1

Views: 13903

Answers (2)

Man Programmer
Man Programmer

Reputation: 5356

You can also use checkbox checked method.

var security = $('input[name="security"]').prop('checked'); /* it will return true or false */

and update your code

var parameters = { 
    'msisdn': $('input[name="msisdn"]').val(),
    'username': $('input[name="username"]').val(),

    'security':security,
    'submit': $('input[name="submit"]').val()
  };

Upvotes: 3

Oscar
Oscar

Reputation: 2047

Use the method is() with property :checked.

  var parameters = { 
   'security':$('input[name="security"]').is(':checked'),
  };

This works for me.

Upvotes: 0

Related Questions