views:

133

answers:

2

Hello all,

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

+1  A: 

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.

Greg
Does the above get effected if its in the $(document).ready(function() { bracket?
Abs
+1  A: 

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;
});
tvanfosson
This actually made things easier as I am going to need many forms and its cut the code I have had to write. Thanks!
Abs