0
I have a class that does some functions to insert and search for information inside the database, I already made the connection, but I do not know how to handle this form, pass the class in the form 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 "
//
// }
}
Follow 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 the comic, but I don’t know where to receive 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');
Create a php file
intermediario
for exampleexemplo.php
and put that same name in the action of yourform
– Thiago Drulla
ai in this file you capture the content that comes from the form and store them in a variable using $_POST
– Thiago Drulla
Usually this class you created just interacts with the database, the best is to create another file that receives the data, validate and then move to the class
DocModel
. Take a look at the standard dao– Costamilam
I recommend using
MVC
. The post would be sent fromview
, would be received and treated oncontroller
and only then passed to themodel
.– Jorge.M