Zen Cart Logo
Forums / General Questions / Help needed to place an onsubmit in checkout_shipping

Help needed to place an onsubmit in checkout_shipping

Locked

Views: 1,060

Results 1 to 4 of 4
This thread is locked. New replies are disabled.
13 Feb 2009, 12:15 AM
#1
sjk1000 avatar

sjk1000

New Zenner

Join Date:
Sep 2008
Posts:
100
Plugin Contributions:
0

Help needed to place an onsubmit in checkout_shipping

Hi all

I need to place an onsubmit in the checkout_shipping form to run a javascript validation function. See the js 'hello' function below as an example. Can someone please tell me where and how I insert the onsubmit to run this? Any help is very much appreciated.
Thanks, Steve

<?php echo zen_draw_form('checkout_address', zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL')) . zen_draw_hidden_field('action', 'process'); ?> <script type="text/javascript"> function hello () {
alert('hello world')
} </script>
13 Feb 2009, 10:34 AM
#2
sjk1000 avatar

sjk1000

New Zenner

Join Date:
Sep 2008
Posts:
100
Plugin Contributions:
0

Re: Help needed to place an onsubmit in checkout_shipping

Anybody? : )

19 Feb 2009, 12:26 PM
#3
sjk1000 avatar

sjk1000

New Zenner

Join Date:
Sep 2008
Posts:
100
Plugin Contributions:
0

Re: Help needed to place an onsubmit in checkout_shipping

I seem to be answering a lot of my own posts recently... not that I'm bitter, just a little twisted. Here's the solution for anyone with a similar problem.

From the checkout_shipping page, the below will open the form with an instruction to call the check_form() function when the submit button is pressed. The key text that I've added is highlighted in red.

<?php echo zen_draw_form('checkout_address',zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'), 'post', 'onsubmit="return check_form(productsObj);"') . zen_draw_hidden_field('action', 'process'); ?> 

The javascript check_form function is below and can be written to a jscript_main.php file in the /mywebsite.com/catalog/includes/modules/pages/checkout_shipping or wherever. If the value of user_quantity_total is less than that of productsObj[key]['qty'], false is returned and the submit fails. I've greyed out the parts that are only relevant to my site. The key text is the if statement.

function check_form(productsObj) {
for (key in productsObj) {
    user_quantity_total = 0;
    for (key2 in productsObj[key]['sellers']){
        user_quantity_total += productsObj[key]['sellers'][key2]['user_selected_quantity'];
    }
    if (user_quantity_total < productsObj[key]['qty'])
    {alert("<?php echo 'This has failed validation'; ?>");
    return false;  }
    }
}

Drop me a pm if anyone needs further clarification on this.
Cheers, Steve

19 Feb 2009, 12:48 PM
#4
sjk1000 avatar

sjk1000

New Zenner

Join Date:
Sep 2008
Posts:
100
Plugin Contributions:
0

Re: Help needed to place an onsubmit in checkout_shipping

Slight revision to add parseInt, highlighted in green, to the site-relevant code:

I seem to be answering a lot of my own posts recently... not that I'm bitter, just a little twisted. Here's the solution for anyone with a similar problem.

From the checkout_shipping page, the below will open the form with an instruction to call the check_form() function when the submit button is pressed. The key text that I've added is highlighted in red.

<?php echo zen_draw_form('checkout_address',zen_href_link(FILENAME_CHECKOUT_SHIPPING, '', 'SSL'), 'post', 'onsubmit="return check_form(productsObj);"') . zen_draw_hidden_field('action', 'process'); ?> 

The javascript check_form function is below and can be written to a jscript_main.php file in the /mywebsite.com/catalog/includes/modules/pages/checkout_shipping or wherever. If the value of user_quantity_total is less than that of productsObj[key]['qty'], false is returned and the submit fails. I've greyed out the parts that are only relevant to my site. The key text is the if statement.

function check_form(productsObj) {
for (key in productsObj) {
    user_quantity_total = 0;
    for (key2 in productsObj[key]['sellers']){
        user_quantity_total += parseInt(productsObj[key]['sellers'][key2]['user_selected_quantity']);
    }
    if (user_quantity_total < productsObj[key]['qty'])
    {alert("<?php echo 'This has failed validation'; ?>");
    return false;  }
    }
}

Drop me a pm if anyone needs further clarification on this.
Cheers, Steve