NomadicJosh Posted August 27, 2013 Share Posted August 27, 2013 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> Third, here is the method from my controller which passes the $_POST['termCode'] to the method of the same name found in the model: public function runTermLookup() { if(!$this->_auth->isUserLoggedIn()) { redirect( BASE_URL ); } $data = array(); $data['termCode'] = isPostSet('termCode'); $this->model->runTermLookup($data); } Lastly, here is the method from my model: 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); } I've been looking at this for hours, so a fresh pair of eyes is greatly appreciated. Thank you. Link to comment https://forums.phpfreaks.com/topic/281621-php-mysql-ajax-call/ Share on other sites More sharing options...
NomadicJosh Posted August 28, 2013 Author Share Posted August 28, 2013 I forget to add that $_POST['termCode'] doesn't seem to get posted when I choose a term from the dropdown option. So, there is something in the jquery that is causing the variable to not be posted for some reason. Link to comment https://forums.phpfreaks.com/topic/281621-php-mysql-ajax-call/#findComment-1447103 Share on other sites More sharing options...
NomadicJosh Posted August 28, 2013 Author Share Posted August 28, 2013 Am I asking the question wrong, or do I need to add more detail of what I am trying to accomplish here? Link to comment https://forums.phpfreaks.com/topic/281621-php-mysql-ajax-call/#findComment-1447222 Share on other sites More sharing options...
NomadicJosh Posted August 29, 2013 Author Share Posted August 29, 2013 I used Google Chrome developer and saw that termCode was stated three times in the form. Once I corrected them, the problem was fixed. Link to comment https://forums.phpfreaks.com/topic/281621-php-mysql-ajax-call/#findComment-1447229 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.