I am working on a project that requires a field in the 'add new record' form to be hidden or unhidden based on an sql valuelist derived select box in the form. I think that this is quite similar to the dynamic master and slave select box filtering utilizing Javascript. I have successfully implemented that type of function in the past, but I can't figure out how to make the field Hide using the variable sent into Javascript.
I have two Tables EQUIPMENT and PRODUCTS. In the PRODUCTS table there is a field called PRODUCT_Bulk_Type that is set to either 1 or NULL (boolean generated from a check box in the Products Form) If the value is 1 then the product can be purchased in bulk quantities. The Code for the Equipment form is supposed to look at the Product ID from the Product Table and the selected ID in the forms valuelist select box and then query the PRODUCT table to find out if PRODUCT_Bulk_Type == 1 or not. If it equals 1 then the form should react by Unhiding a field on the form called EQUIPMENT_Quantity.
Here is the code that I have cobbled together so far, but it is not working. I am actually not sure if I am even approaching the problem correctly.
- Code: Select all
<?php
class tables_EQUIPMENT {
function EQUIPMENT_Installed_Date__default(){
return date('Y-m-d');
}
function block__after_new_record_form(){
import('Services/JSON.php');
$json = new Services_JSON();// convert PHP arrays to javascript arrays.
$record =& Dataface_Application::getInstance()->getRecord();
$productID = $record->val('EQUIPMENT_PRODUCT_ID');
$query = "Select PRODUCT_ID, PRODUCT_Bulk_Type from PRODUCT where PRODUCT_ID='$productID'";
$bulk = mysql_query($query) or die(mysql_error());
echo <<<END
<script language="javascript"><!--
var bulkval = '.$json->encode($bulk).';
var product_field = document.getElementById('EQUIPMENT_PRODUCT_ID');
product_field.onchange = function(){
var selected_product = product_field.options[product_field.selectedIndex].value;
if ( bulkval[PRODUCT_Bulk_Type] == NULL){
document.getElementById(EQUIPMENT_Quantity).style.display = 'none';
}
};
//--></script>
END;
}
// Also place this javascript after an edit record form...
function block__after_edit_record_form(){
return $this->block__after_new_record_form();
}
}
?>
Any guidance you may have to help me figure this out would be greatly appreciated.
Thank you,
- Clayton