Abs
Abs

Reputation: 57916

Change values in form submitted and the JQuery serialize function

Please look at the following code. When the form gets submitted, is it actually submitting the values I have entered i.e. val(50) or at the point it serialzies does it just get data from the form on the actual html page?

// stop all forms from submitting and submit the real (hidden) form#order
$('form:not(#order)').submit(function(event) {
alert($(this).attr('id'));                                
//event.preventDefault();
if($(this).attr('id')==='quick2a'){

    alert('quick2a being submitted');
    //submitQuick2a();
    $('form#order input[name=custom_channels]').val(50);

    var name = 'het-';
    name += $('form#order input[name=platform]').val('astsk');
    name += '-ga-';
    name += $('form#order input[name=license]').val('floating');

    $('form#order input[name=productname]').val(name);

    $.post('/store/cart/add/ajax/', $('form#order').serialize(), function() {
    document.location.href = '/store/checkout';

    });                 
}else{
//
}

I want those values to be set in the form regardless of what is set by the user, am I doing this correctly?

Thanks all

Upvotes: 1

Views: 2767

Answers (2)

tvanfosson
tvanfosson

Reputation: 532435

Why not just construct the data directly instead of stuffing it into a form and then grabbing the values via serialize?

$('form').submit(function(event) {
    if($(this).attr('id')==='quick2a') {
        var data = {
                 'custom_channels': 50,
                 'platform' : 'astsk',
                 'license' : 'floating',
                 'productname' : 'het-astsk-ga-floating'
            };

        $.post('/store/cart/add/ajax/', data, function() {
            document.location.href = '/store/checkout';
        });                                 
    }else{
    //
    }
    return false;
});

Upvotes: 3

Greg
Greg

Reputation: 321578

It gets the values from the HTML page... but by calling .val(...) you're setting the values on the HTML page, so your code will work as you want it to.

Upvotes: 1

Related Questions