Receive a notice when someone leaves a comment on my website

0

I have this code, which is a form to leave comments on my website. I would like to know if there is any way to get that by adding something else code I can get a notice to my email when someone leaves a comment on the web. The code that I leave works well, it does not have failures I only put it so that you can see what I am using. Thanks in advance.

       <div class="container">
   <form method="POST" id="comment_form">
<div class="form-group">
 <input type="text" name="comment_name" id="comment_name" class="form- 
  control" placeholder="Enter Name" />
</div>
<div class="form-group">
 <textarea name="comment_content" id="comment_content" class="form-control" 
  placeholder="Enter Comment" rows="5"></textarea>
</div>
<div class="form-group">
 <input type="hidden" name="comment_id" id="comment_id" value="0" />
 <input type="submit" name="submit" id="submit" class="btn btn-info" 
   value="Submit" />
</div>

       

 add_comment.php:



  <?php

//add_comment.php


  $connect = new PDO('mysql:host=);

   $error = '';
   $comment_name = '';
   $comment_content = '';

      if(empty($_POST["comment_name"]))
     {
     $error .= '<p class="text-danger">Name is required</p>';
    }
      else
    {
     $comment_name = $_POST["comment_name"];
      }

    if(empty($_POST["comment_content"]))
   {
    $error .= '<p class="text-danger">Comment is required</p>';
    }
    else
    {
     $comment_content = $_POST["comment_content"];
     }

     if($error == '')
    {
    $query = "
   INSERT INTO tbl_comment 
 (parent_comment_id, comment, comment_sender_name) 
 VALUES (:parent_comment_id, :comment, :comment_sender_name)
  ";
 $statement = $connect->prepare($query);
 $statement->execute(
  array(
   ':parent_comment_id' => $_POST["comment_id"],
   ':comment'    => $comment_content,
   ':comment_sender_name' => $comment_name
     )
    );
     $error = '<label class="text-success">Comment Added</label>';
     }

     $data = array(
 'error'  => $error
    );

echo json_encode($data);

?>
    
asked by Pablo Santos 29.08.2018 в 02:28
source

1 answer

0

You could send a warning email through some mail function.

$destinatario = '[email protected]';
$asunto = 'Nuevo mensaje en tu web';
$mensaje = 'Mira el artículo x, tienes un mensaje nuevo de Pepito que dice: mensaje.';
$cabeceras = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($destinatario, $asunto, $mensaje, $cabeceras);

Anyway, it's a basic example, you should protect variables, see if your server supports sending with the mail function, shipping limits, etc ...

    
answered by 29.08.2018 / 17:39
source