I've needed a validation script for this addon. I used Jeffs code but fixed the SQL so it works for all products not just the first one...
Ben
When you create the form you'll need to add a onSubmit function like this: -
<?php echo tep_draw_form('cart_quantity', tep_href_link(FILENAME_PRODUCT_INFO, tep_get_all_get_params(array('action')) . 'action=add_product'), 'post', 'onSubmit="return validate_me(this);"'); ?>
Then the javascript is
<script language="javascript"><!--
function validate_me(myform) {
<?php
// jeff added this stuff for radio button validation
$products_verification_query = tep_db_query("select * from " . TABLE_PRODUCTS_OPTIONS . " popt , " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$HTTP_GET_VARS['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "' and popt.products_options_type='3'");
// loop through each option type that has radio buttons
while ($products_verification_array = tep_db_fetch_array($products_verification_query)) {
?>
myOption = -1;
// loop through each element of the radio set
for (i=cart_quantity.elements['<?php echo 'id[' . $products_verification_array['products_options_id'] . ']'; ?>'].length-1; i > -1; i--) {
if (cart_quantity.elements['<?php echo 'id[' . $products_verification_array['products_options_id'] . ']'; ?>'].checked) {
myOption = i; i = -1;
}
}
if (myOption == -1) {
cart_quantity.elements['<?php echo 'id[' . $products_verification_array['products_options_id'] . ']'; ?>'][0].focus();
var alertvar = "You must make a selection from the '<?php echo $products_verification_array['products_options_name'];?>' group.";
alert(alertvar);
return false;
}
<?php
}
// Ben added this stuff for text field validation
$products_verification_query = tep_db_query("select * from " . TABLE_PRODUCTS_OPTIONS . " popt , " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$HTTP_GET_VARS['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "' and popt.products_options_type in ('1', '2')");
// loop through each option type that has radio buttons
while ($products_verification_array = tep_db_fetch_array($products_verification_query)) {
?>
if (cart_quantity.elements['<?php echo 'id[txt_' . $products_verification_array['products_options_id'] . ']'; ?>'].value == "") {
var alertvar = "You must answer the question'<?php echo $products_verification_array['products_options_name'];?>'.";
alert(alertvar);
return false;
}
<?php
}
// Ben added this stuff for checkbox validation
$products_verification_query = tep_db_query("select * from " . TABLE_PRODUCTS_OPTIONS . " popt , " . TABLE_PRODUCTS_ATTRIBUTES . " patrib where patrib.products_id='" . (int)$HTTP_GET_VARS['products_id'] . "' and patrib.options_id = popt.products_options_id and popt.language_id = '" . (int)$languages_id . "' and popt.products_options_type = '4'");
// loop through each option type that has radio buttons
while ($products_verification_array = tep_db_fetch_array($products_verification_query)) {
?>
if (cart_quantity.elements['<?php echo 'id[' . $products_verification_array['products_options_id'] . ']'; ?>'].checked != 1) {
var alertvar = "You must answer the question'<?php echo $products_verification_array['products_options_name'];?>'.";
alert(alertvar);
return false;
}
<?php
}
?>
}
//--></script>