j'ai mis à jour mon code, je n'arrive toujours pas à afficher les commentaires sur la page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
<?php
try {
$connection = new MongoClient();
$database = $connection->selectDB('projet_DB');
$collection = $database->selectCollection('articles');
} Catch(MongoException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$cursor = $collection->find();
?>
<?php session_start();
?>
<html>
<p> this is my article </p>
<p> blablablablablablablablablablablablablablablablablablablablablablablablablabla </p>
<HR width=80% noshade size=4>
<h3> Add comments </h3>
<?php foreach ($cursor as $comment) {
?>
<p> <?php echo $comment['comment_name'].'says...'; ?> </p>
<p> <?php echo $comment['comment'];
}
?>
<form action="" method="post">
<span class="input-label">Name</span>
<input type="text" required name="comment_name"
<br/><br/>
<span class="input-label">Email</span>
<input type="text" required name="comment_email"
<br/><br/>
<textarea class="input-label" name="comment" required rows="5" cols="30"></textarea> <br/><br/>
<input type="hidden" name="article_id" value="<?php echo $article['_id'];?>" />
<input type="submit" name="btn_submit" value="Save"/>
</form>
<?php
function process_comment_if_any_comment_posted ()
{
$comment = get_posted_comment_if_any_and_secure();
if(invalid_or_no_comment($comment)) return;
insert_comment($comment);
}
process_comment_if_any_comment_posted();
function get_posted_comment_if_any_and_secure()
{
$comment = array();
if(! comment_posted()) return $comment;
$email= check_and_secure($_POST, 'comment_email');
$name= check_and_secure($_POST, 'comment_name');
$text = check_and_secure($_POST, 'comment');
if($text == "") return $comment;
$comment['text'] = $text;
$comment['comment_name'] = $name;
$comment['comment_email'] = $email;
global $article;
return $comment;
}
function invalid_or_no_comment($comment)
{
return empty($comment)
|| ! isSet($comment['text'])
|| (trim($comment['text']) == "");
}
function insert_comment($comment)
{
global $database;
$database->comments->insert($comment);
return $comment['_id'];
}
function get_article()
{
$article = array();
return $article;
}
$article = get_article();
function comment_posted()
{
return isSet($_POST['article_id']);
}
function check_and_secure($T, $field)
{
if(! isSet($T[$field])) return "";
return htmlentities($T[$field]);
} |
l'état de la base de donnée après avoir rempli le formulaire :
{ "_id" : ObjectId("552a5ed30faaf28b04b7acdd"), "text" : "voici le commentaire", "comment_name" : "Anis", "comment_email" : "anis@laposte.net" }
Partager