添加/删除元素并通过AJAX更新其属性

时间:2011-01-28 15:43:15

标签: php javascript jquery codeigniter

我正在构建一个小应用程序,允许用户在课程中添加/删除学生。每次我添加/删除课程时,数据库都会更新,我会更新用户的课程总数。当我将一个学生添加到课程中它工作正常,但是当我去删除它们时,我返回0(来自我的PHP模型函数)。我相信它可能是jquery的一个问题,我想我可能需要使用克隆?所以我很抱歉这么长的帖子只是想确保你们有足够的代码来查看。 这是我的jQuery代码,用于添加和删除用户到课程:

function addUserToClass(user)
{

 //creates the a data string to send to the controller with the user's id and the course id
 var postString = "courseid="+$("#course option:selected").val()+"&uid="+user.find('input').val();

 //adds the user to the course list in the database
 $.post('index.php/admin/addUserToCourse',postString);

  $("#courseList").append(user);
  $('#courseList .addUser').text('(-)').removeClass('addUser').addClass('removeUser'); 
}

//removes a user from the selected course in the database as well as on the screen
function removeUserFromClass(user)
{
 var postString = "courseid="+$("#course option:selected").val()+"&uid="+user.find('input').val();

 //removes the user fromt the course in the database
 $.post('admin/removeUserFromCourse',postString);

 $(user).remove();
 $('#usertable').append(user);
 $('#usertable .removeUser').text('(+)').removeClass('removeUser').addClass('addUser'); 



}

这是我的jQuery,它更新了他们所在的课程数量:

function updateUserCourses(uid)
{
var postString = 'uid='+uid.siblings(':input').val();

  $.post('index.php/admin/getUsersCourses', postString , function(data){

   data = jQuery.parseJSON(data);
   uid.siblings('.internalCounter').text(data.internal);
   uid.siblings('.externalCounter').text(data.external);

  })

}

不确定是否需要这个但我的PHP(codeigniter控制器)被调用:

 function getUsersCourses()
 {
  $this->load->model('course_model');
  $courses = $this->course_model->getUsersCourses($_POST['uid']);

  echo json_encode($courses);

 }

我的模特功能:

  function getUsersCourses($uid)
     {
      $external = array();
      $internal = array();
      $final = array();
      //Selects all of the courses the user has been added to
      $usersCourses = $this->db->query("SELECT courseid FROM final_course_list WHERE uid='{$uid}'");

  //If they have courses, proceed to process them
  if ($usersCourses->num_rows() > 0)
  {
   //For every course they have, we get the category of the course
   foreach($usersCourses->result() as $row)
   {
    $courseCat = $this->db->query("SELECT category FROM courses WHERE id = '{$row->courseid}'")->row_array();

    if($courseCat['category'] == 'External Topics')
    {
     array_push($external, $courseCat);
    }
    else if($courseCat['category'] == 'Internal Topics')
    {
     array_push($internal, $courseCat);
    }
   }

   //adds the internal and external amount of classes to one array
   $final['internal'] = count($internal);
   $final['external'] = count($external);
   $result = $final;
  }
  else
  {
   $result = 0;
  }

  return $result;

 }

2 个答案:

答案 0 :(得分:0)

removeUserFromClass不应该阅读:

$.post('index.php/admin/removeUserFromCourse',postString);

答案 1 :(得分:0)

没有比愚蠢的错误更糟糕的事情需要永远修复。我不认为我的JS是我的PHP模型有什么问题。而不是返回0我需要返回一个数组

array("internal"=>0,"external"=>0);