Jump to content

PHP, MySQL, Ajax Call


Go to solution Solved by NomadicJosh,

Recommended Posts

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.