insert() function
creates one row in table
- returns array['rc'] with 'ok' - for success or 'bad' - for failure
- returns array['tableid'] with newly generated Id Sequence for table row inserted
- table column createdate its timestamped
Parameters - insert( tablename, buffer )
- SQL table name
- BUFFER - array, containing multiple values to be inserted
// Example 1 [controllers/controller] - PHP saveTable function
function saveTable() {
$SAVE = $_POST;
$SAVE['orgid'] = (int) Session::get('orgid');
$status = $this->model->db->insert('table', $SAVE);
echo json_encode($status['rc']); // signal javascript
return;
}
Example 2: insert() MVC Process
- triggers an add event which loads a modal (Javascript)
- invoked PHP addClass function then renders addClass form page/view
- upon submitting form, it calls function to save addClass form data.
On callback from php, it notifies success or failure (Javascript) - invoked PHP saveClass function saves addClass form data to database
// Example 2 [public/mvcspa/controller] ADD PROCESS
// Javascript pops up modal, PHP will fetch add class form view
$(document).on('click', '#addclass',
function addclass(e) {
e.preventDefault();
$('#controllerModal').modal({});
$("#controllerContent").load( 'controller/addClass' );
});
// Example 2 [controllers/controller] - PHP presents add class form view
function addClass() {
$orgid = (int) Session::get('orgid');
$ar = $this->model->db->getOne('org', $where=array('orgid'=>$orgid), $sel='orgname' );
$this->view->orgname = $ar['orgname'];
$this->view->render('controller/addclass', false); // shows add class form view
}
// Example 2 [public/mvcspa/controller] - add class form is submitted
$(document).on('submit', '#addClassForm',
function(e) {
e.preventDefault();
var form = $(this);
$.ajax({
url: "controller/saveClass",
type: 'post',
dataType: 'json',
data: form.serialize(),
success:
function( data ) {
// callback: awaiting signal from (saveClass)
if ( data.rc == 'ok' ) {
$('#showclass').trigger('click'); // refresh
Toast.fire('Class Added!','','info');
}
else {
Toast.fire('Class Added failed!','','warning');
return false;
}
$('#controllerModal').modal('hide');
}
});
});
// Example 2 [controllers/controller] - PHP saves data from add class form
function saveClass() {
$SAVE = $_POST; // add class form data
$SAVE['orgid'] = (int) Session::get('orgid');
unset($SAVE['orgname']); // display field only
$status = $this->model->db->insert('class', $SAVE);
echo json_encode($status['rc']); // callback: signals javascript return code status
return;
}