0

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: [email protected]

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
}

}

1 Answer 1

1

Check if the url is correct, maybe trying with this:

var url = "<?php echo base_url()?>index.php/user/updatecomment";

Also try to edit the .htaccess to remove the index.php, it will make easier pointing to a url and also will make it look better.

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

10 Comments

Thanks this help me but when I post the comment the name of the person what is posting is wrong is from another user. Can you help me with that?.
look if you're saving correctly your user id in session every time you login, also clear session variables when you logout, unset_userdata should work, if not, maybe you could try sending the user id through ajax, hope it helps.
Thanks that help I check my ajax. I think I need to add the user in that ajax function.
it wouldn't be necessary to add the user with ajax since you already get the user when this login, but ajax would also work, glad to help! if my answer was helpful, can you please accept my answer? so I can make some reputation here :D
I try but without any success. You are able to change my ajax function in the correct way for see the correct name user?. Thanks for help.
|

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.