Hej zrobiłam dodawanie komentarzy i mam pytanie jak inaczej można "sensownie" przekazywać wartości z inputów? Tylko takim sposobem? Że przez : if(isset($_POST['wyslij'])) {
<html>
<head>
</head>
<body>
<form method="post" action="index.php">
<input type="text" name="author" /><br />
<input type="text" name="email" /><br />
<textarea name="text"></textarea><br />
<input type="submit" name="wyslij" value="Wyslij" />
</form>
</body>
</html>
<?php
require 'database.php';
class Comment {
public $author;
public $email;
public $text;
public $wiadomosc;
public function validate() {
if(isset($_POST['author']) && isset($_POST['email']) && isset($_POST['text'])) {
$this->author = ($_POST['author']);
$this->email = ($_POST['email']);
$this->text = ($_POST['text']);
$this->sendComment();
} else {
$this -> wiadomosc = "Nie wypelniles wszystkich pól formularza.";
}
}
public function sendComment() {
$pdo = Database::connect();
$sql = "INSERT INTO comments(id, author, email, comment) values(".
"NULL, :author, :email, :comment)";
$q = $pdo->prepare($sql);
$q->bindValue(':author', $this->author, PDO::PARAM_STR);
$q->bindValue(':email', $this->email, PDO::PARAM_STR);
$q->bindValue(':comment', $this->text, PDO::PARAM_STR);
$q->execute();
Database::disconnect();
$error = $q->errorInfo();
if ($error[0]) { var_dump($error); }
$this->wiadomosc = "Wiadomość wysłano";
}
}
if(isset($_POST['wyslij'])) {
$comment = new Comment;
$comment-> validate();
echo $comment -> wiadomosc;
}