I need some help in pre-processing online form inputs before submitting and storing them in database. This form is actually done with the help of ChronoForms component in Joomla 3.1. The form is made using HTML and the processing is done using JQuery/JavaScript using the 'Load JS' action inside 'OnLoad' event (for those familiar with ChronoForms).
The simplified form is as follows:
<form name="online_form" id="online_form">
<select name="period_year" id="period_year">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select name="period_month" id="period_month">
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="hidden" name="period" id="period"/>
<input type="submit" id="submit_form"/>
</form>
What I want to do is when users submitted the form, values for period_year and period_month is combined into a text string "x year and y months" and made as value for period. The database will end up storing just period instead of period_year and period_month.
I tried using event listener on submit for the form but it didn't seem to work:
window.addEvent('domready', function() {
$('online_form').addEventListener('submit',
function() {
var period_year = $('period_year').value;
var period_month = $('period_month').value;
$('period').value=period_year+' year and '+period_month+' months';
}
);
}
Another alternative was to use onchange attribute on the select element as follows, but it didn't seem to work for me too:
....
<select name="period_year" id="period_year" onchange="process()">...</select>
<select name="period_month" id="period_month" onchange="process()">...</select>
....
For the head:
function process() {
$('period').value=period_year+' year and '+period_month+' months';
};
What did I do wrong? Or is there other correct ways to do it?