vendredi 10 avril 2015

Form with php and mongodb

my idea : form for comments article


i'm not sur for this line : - $article = array();



  • $database->insert->comments($comment); and for this function get_posted_comment_if_any_and_secure() i'm not sur


thanks to help me



<?php
try {
$connection = new MongoClient();
$database = $connection->selectDB('projet');
$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>

<form action="" method="post">
<span class="input-label">Name</span>
<input type="text" name="comment_name"
class="comment-input"/>
<br/><br/>
<span class="input-label">Email</span>
<input type="text" name="comment_email"
class="comment-input"/>
<br/><br/>
<textarea class="input-label" name="comment" rows="5" cols="30"></textarea> <br/><br/>
<input type="hidden" name="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(
'name' => $_POST['comment_name'],
'email' => $_POST['comment_email'],
'comment' => $_POST['comment'],);

if(! comment_posted()) return $comment;

$name= check_and_secure($_POST, 'comment_name');
$text = check_and_secure($_POST, 'comment');
if($text == "") return $comment;

$comment['text'] = $text;
$comment['comment_name'] = $name;





global $article;

$comment['article'] = $article['_id'];

return $comment;
}




function invalid_or_no_comment($comment)
{
return empty($comment)
|| ! isSet($comment['text'])
|| (trim($comment['text']) == "");
}



function insert_comment($comment)
{
global $database;
$database->insert->comments($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]);
}

Aucun commentaire:

Enregistrer un commentaire