1

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

Abs
  • 56,052
  • 101
  • 275
  • 409

2 Answers2

3

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
  • 524,688
  • 99
  • 697
  • 795
  • 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 Oct 11 '09 at 16:40
1

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
  • 316,276
  • 54
  • 369
  • 333
  • Does the above get effected if its in the $(document).ready(function() { bracket? – Abs Oct 10 '09 at 13:16