开发者

Adding/Removing an element and updating its attribute via AJAX

I'm building a small app that will allow a user to add/remove students from a course. Every time i add/remove a course the database is updated and i update the total number of courses for the user. When i add a student to a course it works fine, but when I go to remove them I am returned a 0 (from my PHP model function). I believe it could be a problem with the jquery, i think i might need to use clone? So I'm sorry for such a long post just wanted to make sure you guys have enough code to look at. Here is my jQuery code that adds and removes the user to the course:

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'); 



}

Here is my jQuery that updates the number of courses they are in:

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);

  })

}

Not sure if you need this but my PHP (codeigniter controller) being called is:

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

  echo json_encode($courses);

 }

And my model function:

  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 F开发者_运维知识库ROM 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;

 }


Shouldn't removeUserFromClass read:

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


Nothing is worse than stupid mistakes that take forever to fix. I don't think there was anything wrong with my JS it was my PHP model. Instead of returning 0 i needed to return an the array

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜