PHP Having a small issue with my add comment function

On my website I have a form looking like this:

<div id="addComment">
<form method="post" action="" id="add_comment">
  <label for="Text">Text</label><br />
  <textarea name="Text" id="Text"> </textarea><br />
  <input type="submit" name="submit" id="submit" />
</form>
</div>

And I'm using codeigniter.

And my controller has this in it:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Image extends CI_Controller {

function index()
{          
    $post['post'] = $this->uri->segment(3);
    $this->load->view('theme/header');
    $this->load->view('view_post', $post);
    $this->load->view('theme/footer');
}

public function add_comment(){

    $text = $this->post('Text');
    $url = $this->uri->segment(3);

    $this->load->model('comment_model');
    $this->load->database();
    $this->load->helper('url');

    $status = "";
    $msg = "";

    if (empty($_POST['Text']))
   {
    $status = "error";
    $msg = "Please enter a title";
   }

   if ($status != "error")
    {  
     $file_id = $this->comment_model->add_comment($text, $url);
     if($file_id == true)
     {
        $status = "success";
        $msg = "Comment successfully added";
     }
     else
     {
        unlink($data['full_path']);
        $status = "error";
        $msg = "Something went wrong when posting the comment, please try again.";
     }

  @unlink($_FILES[$file_element_name]);
   }
   echo json_encode(array('status' => $status, 'msg' => $msg));
   }

 }

 ?>

And in my form "action" I have tried to add "add_comment". But it doesn't seem it helps.. But when I do that. My URL will look like: "website.com/image/index/add_comment/" So I guess it's on the right path. But for some reason.. The comment will not be added to my MySql Database..

And here is my model:

<?php

class Comment_model extends CI_Model {

   public function add_comment($text, $url)
   {
    $currenttime = date('d F Y H:i:s', strtotime('+1 hour'));

  $data = array(
     'url' => $url,
     'comment' => $text,
     'date' => $currenttime
  );
  $this->db->insert('comments', $data);
  return $this->db->insert_id();
   }
  }
?>

To my eyes everything should be working..... Any help?

Thanks in advance.

EDIT:

I figured out something.. When I add "add_comment" to my forms action this happens: When I remove "index" from the url(http://hurhaar.com/image/index/add_comment). It will work.. Then I get "{"status":"error","msg":"Please enter a title"}". But why is my form action adding "index" to the url....?

Answers


you didint take the posted value from form in your code

$file_id = $this->comment_model->add_comment($text, $url); 

first you have to store the posted value of form in the $text and $url

like this

$text=$this->input->post('Text');

second you have to mention action in your form

action="add_comment"

third you cant take post value in codeignitor like this if (empty($_POST['Text'])) you have to use if (empty($this->input->post('Text')))


Need Your Help

angular $resource delete - passing id parameter

javascript angularjs angular-resource

I'm trying to implement an angular service using $resource. I could implement the query method which returns results. Now I'm blocked with delete service.

Is there a way to detect when all child items within a QTreeWidgetItem have been marked 'hidden'?

c++ qt qtreewidget qtreewidgetitem

Is there a preferred way to detect when all of a QTreeWidgetItem's children are marked as hidden? Currently, I'm iterating over all of them every time any of them are hidden.