/**
 * AJAX form responder
 * @param string $formId The HTML form ID
 * @param array $errors The array of the errors (it is used only for generic form processing)
 * @return void
 */
function form_respond($formId, $errors = null)
{
    Form::set('id', $formId);
    $errorStr = '';
    $ajaxResponse = true;
    form_set('error', validation_get('errors'));
    if (is_array($errors) && count($errors)) {
        Form::set('error', $errors);
        $ajaxResponse = false;
        # if no error message and no other message, no need to respond
        $message = Form::get('message');
        if (count(Form::get('error')) == 0 && empty($message)) {
            return;
        }
    }
    $response = array('formId' => Form::get('id'), 'success' => Form::get('success') ? true : false, 'error' => Form::get('error'), 'msg' => Form::get('message'), 'redirect' => Form::get('redirect'), 'callback' => Form::get('callback'));
    if ($ajaxResponse) {
        echo json_encode($response);
    } else {
        echo '<script type="text/javascript">';
        echo 'LC.Form.submitHandler(' . json_encode($response) . ')';
        echo '</script>';
    }
}
示例#2
0
            $post['sheet-dir']         = The directory where the file(s) are saved, encoded by base64_encode()
            $post['sheet-fileName']    = The same value of $post['sheet']
        */
        ### The following commented section works with the sample database ###
        /*
        if (isset($post['photo-postId']) && $post['photo-postId']) {
            # Save file name in db
            # This is only needed when `onUpload` callback is not provided in view.php
            db_insert('post_image', array(
                'pimgFileName' => $post['photo'],
                'postId' => $post['photo-postId']
            ), $useSlug=false);
        }
        
        # Save file name in db
        # This is only needed when `onUpload` callback is not provided in view.php
        db_insert('document', array(
            'docFileName' => $post['doc'],
        ), $useSlug=false);
        */
        $success = true;
        if ($success) {
            form_set('success', true);
            form_set('callback', 'postOutput(' . json_encode($post) . ')');
        }
    } else {
        form_set('error', validation_get('errors'));
    }
}
form_respond('frmAsynFileUpload');
# Ajax response
示例#3
0
    } else {
        # NEW/EDIT
        $validations = array('txtName' => array('caption' => _t('Name') . ' (' . _langName($lc_defaultLang) . ')', 'value' => $txtName, 'rules' => array('mandatory'), 'parameters' => array($hidEditId)));
        if (form_validate($validations)) {
            if ($hidEditId) {
                $data = array('catId' => $hidEditId, 'catName' => $txtName);
                # Get translation strings for "catName"
                $data = array_merge($data, _postTranslationStrings($post, array('catName' => 'txtName')));
                if (db_update('category', $data, false)) {
                    $success = true;
                }
            } else {
                $data = array('catName' => $txtName);
                # Get translation strings for "pptName"
                $data = array_merge($data, _postTranslationStrings($post, array('catName' => 'txtName')));
                if (db_insert('category', $data)) {
                    $success = true;
                }
            }
        } else {
            form_set('error', validation_get('errors'));
        }
    }
    if ($success) {
        form_set('success', true);
        form_set('callback', 'LC.Page.Category.list()');
        # Ajax callback
    }
}
form_respond('frmCategory');
# Ajax response