I am implementing a commenting box with ajax and CodeIgniter.
I want a script where a signed in user comments, and Ajax sends the user_id
and the post
to the controller, the comment is added to the MySQL database and the comment list is then refreshed.
this code below is doing the functionality only i want it done using ajax.
Here is part of my view scenery.php
<?php
$scenery_id=$scenery1['scenery_id'];
echo form_open(('display_scenery/add_comment/'.$scenery_id)); ?>
<div class="input-group" ><!-- input group starts-->
<input type="text" class="form-control" id ="Comment" name ="Comment" placeholder="Comment on Scenery..." maxlength="300" size= "70" required>
<input type ="hidden" name= "Scenery_id" value= " <?php echo $scenery_id?>" />
<button type="submit" id = "submit"class="btn btn-info regibutton" >Post</button>
</div>
</form>
<hr/>
<div id = "comment-box">
<?php
if ($comment==NULL){
//if no scenery comment echo disclaimer
echo " <ul style = ' margin-left: 0px;padding-left: 0px;'> <li style = 'list-style: none; background-color: #fff; padding : 5px 5px 5px 10px; margin: 5px 5px 5px 5px'>";
echo " No scenery Comments";
echo "</li>
</ul>";
} else{
foreach ($comment as $row){
// if the comments are availabe echo them
echo " <ul style = ' margin-left: 0px;padding-left: 0px;'> <li style = 'list-style: none; background-color: #fff; padding : 10px 5px 5px 10px; margin: 5px 5px 5px 5px'>";
echo $row->Comment;
echo "<br/>";
echo "<p style='font-size: 11px; color:#333; padding-top: 5px;'>".date(" D d M Y - H:i:s ",strtotime($row->Date_posted))."By - ". $row->Username. " </p>";
echo $row->Date_added;
echo "</li>
</ul>";
}
}
}
?>
</div>
</div>
<br>
<br>
Here is my Controller display_scenery.php
public function add_comment(){
$this->load->library('form_validation');
$session_data = $this->session->userdata('logged_in');
$User_id= $session_data['User_id'];
$scenery_id = $_POST['Scenery_id'];
$Comment=$_POST['Comment'];
$this->form_validation->set_rules('Comment', 'Comment', 'trim|required');
if($this->form_validation->run() == FALSE)
{
///$error= form_error('Comment');
$this-> session->set_flashdata('error', form_error('Comment'));
redirect ('scenery', 'refresh');
}
else {
//loads the model image_display then redirects to scenery page
$this-> image_display->add_comment( $scenery_id, $Comment,$User);
redirect ('scenery', 'refresh');
}
}