I am working on a
online newspaper/blogging application with
CodeIgniter 3.1.8 and Bootstrap 4. I have decided to add themes to it. The application is
not HMVC, only MVC.
I thought it was a good idea to use the
Twig template engine to the theme(s). For this purpose, I use
CodeIgniter Simple and Secure Twig.
In the application's frontend, I have turned classic Codeigniter views into Twig views. This created a problem with the form used to add comments.
Since I replaced
Code:
<?php echo form_open (base_url('comments/create/') . $post->id, array('id' => 'commentForm', 'class' => 'comment-form ajax-form', 'data-post' => 'comment')); ?>
<input type="hidden" name="postid" value="<?php echo $post->id; ?>">
<input type="hidden" name="slug" value="<?php echo $post->slug; ?>">
<div class="form-group <?php if(form_error('name')) echo 'has-error';?>">
<input type="text" name="name" id="name" class="form-control" placeholder="Name" data-rule-required="true">
<?php if(form_error('name')) echo form_error('name'); ?>
</div>
<div class="form-group <?php if(form_error('email')) echo 'has-error';?>">
<input type="email" name="email" id="email" class="form-control" placeholder="Email" data-rule-required="true">
<?php if(form_error('email')) echo form_error('email'); ?>
</div>
<div class="form-group <?php if(form_error('message')) echo 'has-error';?>">
<textarea name="message" id="message" cols="30" rows="5" class="form-control" placeholder="Comment" data-rule-required="true"></textarea>
<?php if(form_error('message')) echo form_error('message'); ?>
</div>
<div class="form-group">
<input type="submit" value="Comment" class="btn btn-block btn-md btn-success">
</div>
<?php echo form_close(); ?>
with
Code:
<form id="commentForm" action="{{base_url}}comments/create/{{post.id}}" method="post">
<input type="hidden" name="postid" value="{{post.id}}">
<input type="hidden" name="slug" value="{{post.slug}}">
<div class="row uniform">
<div class="form-controll 6u 12u$(xsmall)">
<input type="text" name="name" id="name" placeholder="Name" data-rule-required="true" />
</div>
<div class="form-controll 6u$ 12u$(xsmall)">
<input type="email" name="email" id="email" placeholder="Email" data-rule-required="true" />
</div>
<div class="form-controll 12u$">
<textarea name="comment" rows="6" id="message" data-rule-required="true"></textarea>
</div>
<div class="12u$">
<input type="submit" value="Add comment" class="button special fit" />
</div>
</div>
</form>
create method inside the
Comments controller fails, as there is no longer a value for the variable.
I use the slug to identify the post, because that's what I have in the url. I get the post's
id from the form (the hidden input).
Code:
public function create($post_slug){
$post_id = $this->input->post('postid'); // this has become useless
$post_slug = $this->input->post('slug');
$data = $this->Static_model->get_static_data();
$data['base_url'] = base_url("/");
$data['pages'] = $this->Pages_model->get_pages();
$data['categories'] = $this->Categories_model->get_categories();
$data['posts'] = $this->Posts_model->sidebar_posts($limit=5, $offset=0);
$data['post'] = (object) $this->Posts_model->get_post($post_slug);
$data['comments'] = $this->Comments_model->get_comments($post_id);
$data['tagline'] = "Add comment";
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|trim|valid_email');
$this->form_validation->set_rules('message', 'Comment', 'required');
$this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');
$this->twig->addGlobal('maincss', base_url('themes/caminar/assets/css/main.css'));
if($this->form_validation->run() === FALSE) {
$this->twig->addGlobal('singlePost','themes/caminar/templates/singlepost.twig');
$this->twig->display('themes/caminar/layout', $data);
} else {
$this->Comments_model->create_comment($post_id);
}
}
The posting of the form data is done via jQuery Ajax:
Code:
$("#commentForm").validate({
rules: {
email: {
email: true
}
},
submitHandler: function(form) {
var form = $("#commentForm"),
$fields = form.find('input[type="text"],input[type="email"],textarea'),
url = form.attr('action'),
data = form.serialize();
$.ajax({
type: "POST",
url: url,
data: data,
success: function() {
$('#comment_add_msg').text("Your comment will be published after approval")
.slideDown(250).delay(2500).slideUp(250);
$fields.val('');
},
error: function() {
$('#comment_add_msg').removeClass('alert-success').addClass('alert-danger')
.text("Sorry, we could not add your comment")
.slideDown(250).delay(2500).slideUp(250);
}
});
}
});
How can I pass the post's id from comments.twig to the
Comments controller?