Tyler Richardson
Tyler Richardson

Reputation: 309

Using AJAX to send a checkbox value

I have an ajax script that sends data. I only want it to send the data if the input is checked. for each input i have defined a specific value. How can i only have the data input defined if it is checked? or is there a better (simple) way to do it? All of this is within a dialog.

            "Send": (function() {
                        var data = {
                            name: $("input#name").val(),
                            phone: $("input#phone").val(),
                            email: $("input#email").val(),
                            interest1: $("input#interest1").val(),
                            interest2: $("input#interest2").val(),
                            interest3: $("input#interest3").val(),
                            yesA: $("input#yesA").val(),
                            noA: $("input#noA").val(),
                            yesB: $("input#yesB").val(),
                            noB: $("input#noB").val(),
                            interest_1:
                                if("input#interest_1").is(':checked')) {
                                    $("input#interest_1").val(),
                                }
                            interest_2: $("input#interest_2").val(),
                            interest_3: $("input#interest_3").val(),
                            architectY: $("input#architectY").val(),
                            architectN: $("input#architectN").val(),
                            plansY: $("input#plansY").val(),
                            plansN: $("input#plansN").val(),
                            datepicker: $("input#datepicker").val(),
                            alt_date: $("input#alt_date").val(),
                        }
                        var $dialog=$(this)
                        $.ajax({
                            type: "POST",
                            url: "mailer.php",
                            data: data,
                            success: function () {
                                $dialog.dialog("close");
                            }
                        });
                        return false;  


                              }),

An abeviated version of the form looks like this: note* i do not want to use radio buttons because users should be able to choose multiple options.

     <div id="dialog">
      <form method="post" action="mailer.php">
       <input type="checkbox" id="interest_1" value="option A"/>Option A
       <input type="checkbox" id="interest_2" value="option B"/>Option B
       <input type="checkbox" id="interest_3" value="option C"/>Option C
     </form>
      </div>

Upvotes: 0

Views: 7730

Answers (1)

Matt Burland
Matt Burland

Reputation: 45135

Your question's a little vague, but I assume your problem is with this:

interest_1:
    if("input#interest_1").is(':checked')) {
        $("input#interest_1").val(),
    }

So, if I understand this correctly, you want if input#interest_1 is checked, set interest_1 in your ajax data. In that case, you can use an immediately invoked function expression (IIFE) like this:

interest_1: (function() { 
    if($("input#interest_1").is(":checked")) {
        return $("input#interest_1").val();
    }
})(),

Now if interest_1 is checked, then the function will return its .val(). Otherwise it will be void.

Upvotes: 2

Related Questions