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.