2018-09-03, 03:20:56
If your items do not contain complex fields like the 'file upload' etc, I recommend that you create your own form. Creating a simple custom form is quite faster than using the API to render markup:
Now you can process and save the data after sending the form. You say you already have ImForms in use, so you can just get the user input directly from the input variable `$imforms->input->post->field_name`, instead of the using PHP superglobal $_POST, for example:
Ps. The code above should be placed somewhere at the top of your template file before the output is done.
More information about using ItemManager can be found on the website: https://ehret-studio.com/articles/itemmanager/
Code:
<p><?php echo $msg ?></p>
<form action="./" method="post" ...>
<input type="text" name="title"><br>
<textarea name="content"></textarea><br>
...
<input type="hidden" name="action" value="save-item">
<button type="submit">Save</button>
</form>
Now you can process and save the data after sending the form. You say you already have ImForms in use, so you can just get the user input directly from the input variable `$imforms->input->post->field_name`, instead of the using PHP superglobal $_POST, for example:
PHP Code:
$msg = '';
if($imforms->input->post->action == 'save-item') {
if(saveItem($imforms, $msg)) {
Util::redirect('./');
}
}
function saveItem($imforms, & $msg) {
$input = $imforms->input;
$imanager = imanager();
// validate/sanitize user input
$input->whitelist->title = $imanager->sanitizer->text($input->post->title);
if(!$input->whitelist->title) {
$msg = 'Error: Title field empty ...';
return false;
}
$input->whitelist->content = $imanager->sanitizer->textarea($input->post->content);
if(!$input->whitelist->content) {
$msg = 'Error: Enter a content ...';
return false;
}
// Create new item (just change the category id)
$category_id = 5;
$newItem = new Item($category_id);
$newItem->name = $input->whitelist->title;
$newItem->setFieldValue('content', $input->whitelist->content);
$newItem->active = true;
if($newItem->save()) { return true; }
return false;
}
Ps. The code above should be placed somewhere at the top of your template file before the output is done.
More information about using ItemManager can be found on the website: https://ehret-studio.com/articles/itemmanager/