9 Replies - 951 Views - Last Post: 15 May 2012 - 10:09 AM

#1 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Problem with jquery function

Posted 01 May 2012 - 07:11 AM

userList = function userList()
		{
		   $('#users_list2').load('user_list.php');
								
	  }

<?php
//user_list.php
include "includes/connectDb.php";


//GRAB DB DATA
$sql = "SELECT display_name,is_logged FROM members WHERE is_logged = '1'";
$result = mysql_query($sql);
//$num = mysql_num_rows($result);



while($row = mysql_fetch_assoc($result))
{
	
	
		echo  $row['display_name'] . "<br/>" ;

}





?>


This block of code should be run when an anchor link on the page is clicked, it does run, but after working properly twice or three times it stops working and leaves the "#users_list2" container empty even after a refresh of the page. Any help or advice on a workaround or a better way to do this would be much appreciated. The real purpose of this is just to generate a list with all the currently logged in member names from the database and load them into a div.

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with jquery function

#2 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Problem with jquery function

Posted 01 May 2012 - 11:40 AM

When you are using load, all the existing HTML is wiped out and the fresh HTML is loaded. So if at some point of time no user is logged in, it will show nothing.
Was This Post Helpful? 0
  • +
  • -

#3 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Re: Problem with jquery function

Posted 02 May 2012 - 01:10 AM

Thanks for your reply, the problem occurs even when 5 people are still logged in though.
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Problem with jquery function

Posted 02 May 2012 - 05:12 AM

Is the ajax call successfull?
Check in the browser in the xhr section under Network (if you use chrome for example).
Any javascript errors maybe? (check the console section.
Was This Post Helpful? 0
  • +
  • -

#5 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Re: Problem with jquery function

Posted 02 May 2012 - 07:57 AM

Yes it does work the first two or three times around, but when trying again after say four members logged in it stops loading to the div.
Was This Post Helpful? 0
  • +
  • -

#6 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

Re: Problem with jquery function

Posted 02 May 2012 - 08:01 AM

I really cannot find any problem with this code. Perhaps the problem lies elsewhere. After 3 or 4 times, when you are saying it stops working, have you seen the activity in Firebug or something?
Was This Post Helpful? 0
  • +
  • -

#7 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Problem with jquery function

Posted 02 May 2012 - 02:14 PM

How are you calling the userList variable/function? I'd say the problem lies not in the PHP, but in the JS where the .load function is activated/executed.
Was This Post Helpful? 0
  • +
  • -

#8 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Re: Problem with jquery function

Posted 03 May 2012 - 05:16 AM

@ e_i_pi i call it with an "a" link
<a href="#" onclick="userList();">refresh list</a>

Like the previous.
Was This Post Helpful? 0
  • +
  • -

#9 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 795
  • View blog
  • Posts: 1,681
  • Joined: 30-January 09

Re: Problem with jquery function

Posted 03 May 2012 - 05:43 PM

Ordinarily with an onclick() call, I return false on the call to override the native onclick method of the anchor. Given that your href is #, I'm not sure that it would help at all. Also, if I need a global function, I just declare the function rather than assign it to a variable:
function userList()
{
	$('#users_list2').load('user_list.php');
	return false;
}


I'm not sure if changing the function to this would help or not (not able to test at the moment).

In your user_list.php file, have you tried uncommenting the $num line, and logging the value, to see if the file is actually being called? You can check the call via something like Firebug (in Firefox), or by putting this line in your PHP after the $num line:
error_log(var_export($num, true));


Was This Post Helpful? 1
  • +
  • -

#10 kabuto178  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 130
  • Joined: 29-January 11

Re: Problem with jquery function

Posted 15 May 2012 - 10:09 AM

I have tried that suggestion, but to no avail yet. Still pondering
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1