0

Basically I have a form that submits notes on work orders. I've got it posting but now its not passing checkbox data no matter if the box is checked it still goes thru as if its not. It passed a "on"

Object { result: "on" }variable.

my ajax code.

  /*global $*/
    $('#noteform').submit(function(e) {
        e.preventDefault();

        var csrf_token = "{{ csrf_token() }}";
        var data = {};
        var Form = this;

        $.each(this.elements, function(i, v) {
            var input = $(v);
            data[input.attr("name")] = input.val();
            delete data["undefined"];
        });

        $.ajax({
            type: 'POST',
            url: '/ticket/add/note',
            dataType: 'json',
            contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(data),
            context: Form,
            success: function(callback) {
                console.log(callback);
                $('#notes').load(' #notes');
                $('#notecount').load(' #notecount');
                $('input[type=text], textarea').val('');
                $('input[type=checkbox]').prop("checked", false);
            },
            error: function() {
                alert('error posting!');
            }
        });

        $.ajaxSetup({
                beforeSend: function(xhr, settings) {
                    if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
                        xhr.setRequestHeader("X-CSRFToken", csrf_token);
                    }
                }
        });

    });
Tego101
  • 1
  • 2
  • Please update your post to show code and what you've tried so far so we can help you. – SteveB Nov 28 '17 at 04:24
  • Just updated. Hope you can help – Tego101 Nov 28 '17 at 04:32
  • Check out the following post on how to get the value from a checkbox in jQuery: https://stackoverflow.com/questions/2834350/get-checkbox-value-in-jquery – SteveB Nov 28 '17 at 04:39
  • @SteveB , I've seen that I just honestly want to pass a 0 or 1 so I can save it to a row. It basically distinguishes if the note is private or not I dont want to create a if statement with similar outputs just to achieve 1 thing. – Tego101 Nov 28 '17 at 04:41
  • Is there a reason you are sending JSON-stringified manually-serialized form data, and not the more usual `var data = $(this).serialize()`? – Roamer-1888 Nov 28 '17 at 04:42
  • Show how you generate the checkbox (if you do not give it a value, the its default is "on" in accordance with the standards) –  Nov 28 '17 at 04:44
  • You may need to do something special for checkboxes. If you check if the element is a checkbox using $('#myinput').is(':checkbox'), then you can handle the checkbox different from other elements and return the 0 or 1 you want. – SteveB Nov 28 '17 at 04:46
  • @Roamer-1888 JSON.serialize is not a function – Tego101 Nov 28 '17 at 05:04
  • @StephenMuecke Regular html is how I'm calling it. Am I doing something wrong? – Tego101 Nov 28 '17 at 05:05
  • Er, correct, `JSON.serialize` is indeed not a function. I know that. – Roamer-1888 Nov 28 '17 at 05:11
  • You have not given it a `value` attribute, so when you use `input.val()` it returns `"on"` (the default). But in anycase, you need to check if the `type` is a `checkbox` and the use `.is(':checked')` to check is its `checked` and based on that, return `0` or `1` –  Nov 28 '17 at 05:19
  • And regarding the comment by @Roamer-1888 - its `data = $('form').serialize();` which will correctly serialize all your inputs to a query string - but you need to remove the `contentType` option so it uses the default `'application/x-www-form-urlencoded; charset=UTF-8'` –  Nov 28 '17 at 05:22
  • @StephenMuecke is there a way to separate the data instead of its being an array? Im new to this and it's only 3 being passed body, is_private, and workorderid – Tego101 Nov 28 '17 at 05:45
  • What do you mean _an array_ - nothing in your code is creating an array (just an object with properties representing the name/value pairs of your inputs). And I have no idea what _body, is_private, and workorderid_ are - you have not shown any of your html. –  Nov 28 '17 at 05:53

0 Answers0