0

I'm having issues using .serialize() on the following form:

        <form id="cfgedit">
        <div class="card hoverable">
            <div class="card-content">
                <div class="row center-align">
                    <div class="col s12 m4 input-field"><input type="text" id="bpid" name="bpid" value="" /><label for="bpid">Business Partner</label></div>
                    <div class="col s12 m4 input-field"><input type="text" id="serviceid" name="serviceid" value="" /><label for="serviceid">Service ID</label></div>
                    <div class="col s12 m4 input-field"><input type="text" id="servicetype" name="servicetype" value="" /><label for="servicetype">Service Type</label></div>
                </div>

                <div class="row teal lighten-5">
                    <div class="input-field col s12">
                        <textarea id="smstext" name="smstext" class="materialize-textarea flow-text" length="160"></textarea>
                        <label for="smstext">SMS Text</label>
                    </div>
                </div>

                <div class="row">
                    <div class="col s12 m6 input-field right-align"><input type="text" id="url" name="url" value="" /><label for="url">Url destinazione</label></div>
                    <div class="col s12 m6"><strong>Validit&agrave;:</strong><br />
                        <input type="number" name="validityD" min="0" value="1"> d
                        <input type="number" name="validityH" min="0" max="23" value="0"> h
                        <input type="number" name="validityM" min="0" max="59" value="0"> m
                    </div>
                </div>
            </div>
            <div class="card-action">
                <a class="btn btn-large waves-effect red" href="/admin">Cancel</a>
                <a class="waves-effect btn-large right" id="persist">Save</a>
            </div>
        </div>
    </form>

I'm using $.ajax to post the form to my back-end, bound to #persist button click.

$.ajax({
    type: "POST",
    url: "/admin/config/add",
    data: $("#cfgedit").serialize(),
    success: function(data) {
            // display ok
    },
    error: function(data)
    {
            // display not ok
    }
});

Now the issue I'm facing is with the $.ajax data. Because using Chrome's console and typing $("#cfgedit").serialize(); I do get the correct form serialization:

"bpid=TEST_BP&serviceid=TEST_SID&servicetype=TEST_ST&smstext=SMS_TEXT&url=http%3A%2F%2Ftest.url&validityD=1&validityH=2&validityM=3"

But if I send the form, that same serialize returns only

"smstext=SMS_TEXT"

The fields I'm expecting in output all have name attribute set.

2
  • And what you get on server side? Commented Apr 4, 2017 at 9:42
  • 1
    @MayankPandeyz I was just getting smstext, but I've just posted the answer myself. I didn't know serialize is not serializing disabled inputs. my fault. Commented Apr 4, 2017 at 9:49

1 Answer 1

1

solved: I was disabling form inputs to don't let user modify stuff while performing the ajax request:

$("input").attr("disabled", "disabled");

saving values before disabling the inputs solved the problem.

var values = $("#cfgedit").serialize();

.serialize() is not serializing disabled fields.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.