I am not a jquery guru in any sense, so I am seeking some help with the below code. What I am trying to do, is populate two input fields, based on what is selected from a dropdown field via a database call.
When I select a term from the dropdown, I want the term start date and term end date to populate in the appropriate fields. First here is my form:
<form class="form-horizontal margin-none" action="<?=BASE_URL;?>form/runSection/" id="validateSubmitForm" method="post" autocomplete="off">
<div class="control-group">
<label class="control-label"><font color="red">*</font> <?php _e( _t( 'Term' ) ); ?></label>
<div class="controls">
<select style="width:100%;" name="termCode" id="select2_10" required>
<option value=""> </option>
<?php table_dropdown('term', 'termCode', 'termName'); ?>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label"><?php _e( _t( 'Term Start/End' ) ); ?></label>
<div class="controls">
<input type="text" name="termStartDate" id="termStartDate" disabled class="span6" required />
<input type="text" name="termEndDate" id="termEndDate" disabled class="span6" required />
</div>
</div>
Second here is the javascript section:
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#select2_10').live('change', function(event) {
$.ajax({
type : 'POST',
url : '<?=BASE_URL;?>section/runTermLookup/',
dataType: 'json',
data : $('#validateSubmitForm').serialize(),
cache: false,
success: function( data ) {
for(var id in data) {
$(id).val( data[id] );
}
}
});
});
});
</script>
And third, here is the database info. How it works is that the url in the javascript code above is calling a method in a controller called runTermLookup which then routes to a method in the model with the same name.
public function runTermLookup($data) {
$bind = array(":term" => $data['termCode']);
$q = DB::inst()->select( "term","termCode = :term","termID","termStartDate,termEndDate", $bind );
$r = $q->fetch(\PDO::FETCH_ASSOC);
$json = array( 'input#termStartDate' => $r['termStartDate'], 'input#termEndDate' => $r['termEndDate'] );
echo json_encode($json);
}
Any help with figuring out what I am missing is greatly appreciated. Thank you.