I'm using CodeIgniter. I created a forum but when I try to comment on an existing discussion I receive this error: POST http://jaku0260.keaweb.dk//index.php/user/updatecomment 404 Not Found 165ms
.
On the same site you can actually try for yourself:
User: jakub@mail.com
Pass: delfino
Here is the code for the form:
<input type="text" class="form-control" id="<?php echo $post->postid;?>" placeholder="share your
thoughts about this">
<span class="input-group-btn">
<button class="btn btn-default" onclick="hello(<?php echo $post->postid;?>);"
type="button">Share</button>
<script type="text/javascript">
function hello(id){
var data=document.getElementById(id).value;
if(data !=""){
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "<?php echo base_url()?>/index.php/user/updatecomment";
var vars = "postid="+id+"&comment="+data;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById(id+"comment").innerHTML= "<li style='list-style:none;margin-top: 10px;'><div class='row'><div class='col-lg-1'></div><div class='col-lg-10'><div class='input-group'><span class='input-group-btn'><img src='<?php echo base_url();?>assets/user/<?php echo $profpic;?>' class='commentboxpic'></span><span><span class='personnameclass'>Asad Ullah</span><span style='padding-left:5px;'>"+ data+"</span></span></div><!-- /input-group --></div><!-- /.col-lg-6 --></div><!-- /.row --></li>"+document.getElementById(id+"comment").innerHTML;
document.getElementById(id).value="";
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
}
}
</script>
Here the function control:
function updatecomment(){
if($this->session->userdata('userid')){
$this->load->model('forum_model');
$id=$this->input->post('postid');
$userID=$this->session->userdata('userid');
$comment=$this->input->post('comment');
$this->forum_model->insertcomment($userID,$id,$comment);
echo 1;
}
else{
echo 0;
}
Model:
function getkomments($id){
$this->db->where('forumid',$id);
$this->db->from('forumcomment');
$this->db->join('user','forumcomment.userid=user.userid');
$query=$this->db->get();
if($query->num_rows>=1)
return $query->result();
return NULL;
}
function insertcomment($userID,$id,$comment){
$data=array(
'userid'=>$userID,
'forumid'=>$id,
'comment'=>$comment
);
$this->db->insert('forumcomment',$data);
return 1;
}
Thanks for help.
ajax:
function hello(id){
var data=document.getElementById(id).value;
if(data !=""){
var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
var url = "<?php echo base_url()?>index.php/user/updatecomment";
var vars = "postid="+id+"&comment="+data;
hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
document.getElementById(id+"comment").innerHTML= "<li style='list-style:none; margin-top: 10px;'><div class='row'><div class='col-lg-1'></div><div class='col-lg-10'><div class='input-group'><span class='input-group-btn'><img src='<?php echo base_url();?>assets/user/5.jpg' class='commentboxpic'></span><span><span class='personnameclass'></span><span style='padding-left:5px;'>"+ data+"</span></span></div><!-- /input-group --></div><!-- /.col-lg-6 --></div><!-- /.row --></li>"+document.getElementById(id+"comment").innerHTML;
document.getElementById(id).value="";
}
}
// Send the data to PHP now... and wait for response to update the status div
hr.send(vars); // Actually execute the request
}
}