0

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>
6
  • You could use an if statement Commented Dec 25, 2013 at 22:47
  • Can you post your html? Commented Dec 25, 2013 at 22:48
  • I am trying to use an i statement but i isnt working. Also i updated my question with an abreviated version of the form. the html is correct, i am sure about that. I just want the value of each of the options to be assigned to the data and sent to my mailer.php if the box is checked Commented Dec 25, 2013 at 23:22
  • You could use $('form').serialize() to generate the data. Commented Dec 25, 2013 at 23:30
  • @Daiwei what would that do to my checkbox values? how would it compute the inputs? Commented Dec 25, 2013 at 23:39

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.