1

I asked this question 2 days ago, got 1 answer, but I didn't understand it. So, I am asking you if you guys have a better/easy option.

Look at this code snippet:

<?php
foreach($array as $key=>$value)
       {
?>
           <div>
               <input type="text" name="comment" id="comment" onkeypress="showUser()"/>
               <input type="hidden" name="id" id="id" value="<?php echo $value['comment'];?>"/>
               <div id="comment"></div>
           </div>
<?php
       }
?>

Here, there are some <div> items, each having their own "textfield"s. What I want, my users will give their comments in that "textfield", and I'll get the corresponding comment-id from that hidden field. And when a user inputs a comment, this comment will be stored in the database and shown to the user(in the "comment" <div>), using that "id". I thought to use a normal <form> and let the user "submit" (a submit button) the comment. But that is time consuming, the whole page will be reloaded. So, I think AJAX is better for this thing. Its like "commenting" part of "Facebook".

This is my AJAX code:

<script type="text/javascript">
    function showUser()
    {
        $(this).keypress(function(e){
            if(e.which && e.which==13)
              {
                  $.post("<?php echo base_url();?>help/ajax_work_comment",
                  {
                      comment:this.value,
                      id:$(this).next('#id').attr('value');
                  })
              }
            else
              {}
        });
    }
</script>

Remember, this is a Code Igniter project, so "help" is a Controller and ajax_work_comment is a function in that Controller. Here is ajax_work_comment() function:

public function ajax_work_comment()
{
    $comment=$_POST["comment"];
    $id=$_POST["id"];
    $this->load->model('help/model');

    $this->model->ajax_work_comment($comment,$id);
    echo $comment;
}

I know its very complex. Actually, I am a new web developer, that's why it became that complex and disgusting. Please help me.

5
  • 2
    What's the question here? Does this work? Not work? What are you asking us? Commented May 17, 2013 at 15:32
  • +1 to the above. What's the issue? If you're storing the AJAX in a separate directory you will need to modify your .htaccess so it doesn't redirect to the index.php of CodeIgniter by default. Commented May 17, 2013 at 15:34
  • @RocketHazmat this code is not working, I can't storing users' comments in the database by this code. I think the problem is in the AJAX code. Commented May 18, 2013 at 7:32
  • @monkeymatrix I am not doing this, the .htaccess is alright and this AJAX code snippet is in a "view" file. Commented May 18, 2013 at 7:33
  • @user2387319 please paste the error here and if no error is occuring still comments are not storing then please paste your model code here Commented May 18, 2013 at 7:40

2 Answers 2

1

check for javascript errors.

probably your js code has to be in the document ready function

$(document).ready(function() {
};

also have a look at http://learn.jquery.com/code-organization/concepts/

Sign up to request clarification or add additional context in comments.

Comments

0

$_POST["whatever"]; will probably fail.

Use $this->input->post('comment'); and $this->input->post('id'); instead.

And please provide more info next time.

2 Comments

Ok, I am gonna try this. Can you tell me what else do you need to see ? @Ahmed D. Sherif
can u download firebug "on firefox" and send me the post data "from network tab" and the received data and add print_r($this->input->post()) at the start of the function ajax_work_comment and get me the data again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.