I have a class that does some functions to insert and search information inside the database, I made the connection, but I do not know how to handle this form, pass the class in the form's action?
Follow my class:
<?php
require_once "bcrypt.class.php";
require_once "db.class.php";
require_once "helpers.class.php";
/**
*
*/
class DocModel
{
protected static $Helpers;
protected static $pdo;
function __construct(argument)
{
self::$Helpers = new Helpers;
self::$pdo = Database::connect();
}
public function addDoc($title, $text){
$sql = "INSERT INTO doc_model (id, title, text) VALUES (DEFAULT, :title, :text);";
$st = self::$pdo->prepare($sql);
$st->bindParam(':title', $title);
$st->bindParam(':text', $text);
$st->execute();
if ($st->rowCount() > 0) {
return true;
} else {
return false;
}
}
public function getDoc(){
$sql = "SELECT * FROM doc_model ORDER BY id DESC;";
$st = self::$pdo->prepare($sql);
$st->execute();
if ($docmodel = $st->fetchAll(PDO::FETCH_ASSOC)) {
return $docmodel;
} else {
return false;
}
}
// public function removeDoc($title, $text){
// $sql = "DELETE FROM doc_model where "
//
// }
}
Here is my HTML form:
<form method="post" class="form-horizontal" action="?">
<div class="box-body">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Título *</label>
<div class="col-sm-9">
<input name="title" type="text" class="validate[required] form-control" placeholder = "Título do documento modelo" value="<?php echo ((isset($array['title']))?$array['title']:"")?>"/>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Texto *</label>
<div class="col-sm-9">
<textarea id="message" name="message" class="form-control" placeholder = "Texto da mensagem" ><?php echo ((isset($array['message']))?$array['message']:"")?></textarea>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary"><i class="fa fa-floppy-o"></i> Salvar</button>
</div>
<input type="hidden" name="tab" value="tab_1">
</form>
I want to save the data of this form in bd, but I do not know where to get the $ _POST, whether it would be in the class or in an action.
Intermediate script:
<?php
if (!session_id()) {
session_start();
}
define('__ROOT__', dirname(__FILE__));
require_once (__ROOT__.'/classes/user.class.php');
require_once (__ROOT__.'/classes/helpers.class.php');
require_once (__ROOT__.'/config.php');
require_once (__ROOT__.'/classes/docmodel.class.php');
if (!isset($_SESSION['USER']) OR !User::checkSession($_SESSION['USER'])) {
header ('Location: sair');
exit;
}
if(isset($_SESSION['USER']['type']) AND $_SESSION['USER']['type'] > 2) {
header ('Location: painel');
exit;
}
$config['page-title'] = 'Documentos Modelo';
$User = new User;
$Helpers = new Helpers;
$DocModel = new DocModel();
$title = $_POST['title'];
$text = $_POST['message'];
var_dump($title, $text);exit();
$DocModel->addDoc($title, $text);
include_once ('templates/header.tpl.php');
include_once ('templates/sidebar.tpl.php');
include_once ('templates/breadcrumb.tpl.php');
include_once ('templates/modelos.tpl.php');
include_once ('templates/footer.tpl.php');