我正在使用CodeIgniter。我创建了一个论坛,但当我尝试对现有讨论发表评论时,我收到了此错误:POST http://jaku0260.keaweb.dk//index.php/user/updatecomment 404 Not Found 165ms
。
在同一网站上,您实际上可以try for yourself:
用户:jakub@mail.com
通过:delfino
以下是表单的代码:
<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>
这里是功能控制:
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;
}
型号:
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;
}
感谢您的帮助。
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
}
}
答案 0 :(得分:1)
检查网址是否正确,可能尝试使用此方法:
var url = "<?php echo base_url()?>index.php/user/updatecomment";
还尝试编辑.htaccess以删除index.php,它会更容易指向一个网址,也会让它看起来更好。