Welcome Guest, Not a member yet? Register   Sign In
jquery $.post server cpu with controller function
#1

[eluser]rickrude[/eluser]
Hi there.

I have just in the last couple of weeks started using codeigniter, and better yet, yesterday I started learning about jquery. I am a complete noob so here goes.

I am having trouble with using jquery $.post to retrieve data via a controller function. I am trying to follow the MVC standards.

Code:
$("a").click(function(show)
                {  
                    var stuff = $(this).text();
                    show.preventDefault();
                    $.post('users/getusertest', { user: stuff },
                        function(output){
                            $('#usage').html(output);
                        });
                });

This works, but it consumes 100% server CPU for at least 5 seconds per click. I have narrowed it down to this -
Code:
$.post('users/getusertest', { user: stuff }
if I create a test.php file for processing the $.post, I don't get cpu problem!

Code:
$.post('test.php', { user: stuff }

TEST.PHP
Code:
<?php

echo "user test ".$_POST['user'];
?>

I am trying to keep the code as clean as possible.


Here is the controller with the getusage function:

Code:
<?php

class Users extends CI_Controller
{
    function __construct() {
        parent::__construct();
        $this->load->model('getdata_model');
        
    }
function index()
    {
        $this->view();
    }
    function view()
    {
        
       ... lots of calls to the model
    }
      
    function getusertest()
    {
        echo "RETURNED ".$_POST['user'];
        
    }
}
?>


The reason I am using jquery to do the post stuff is because I am trying to avoid page refreshes. I am open to criticism, not even sure if this is the best way.

cheers.
#2

[eluser]rickrude[/eluser]
Just found out that this happens when using IE and chrome BUT NOT IPHONE ?
#3

[eluser]InsiteFX[/eluser]
Code:
$.post(<?php echo base_url();?>+'users/getusertest', { user: stuff },
#4

[eluser]rickrude[/eluser]
Thanks for the suggestion, this is what I changed it to -
Code:
$.post('<?php echo base_url();?>users/getusertest', { user: stuff }

but it's still a cpu hog.

cheers.
#5

[eluser]skunkbad[/eluser]
[quote author="rickrude" date="1336528006"]Thanks for the suggestion, this is what I changed it to -
Code:
$.post('<?php echo base_url();?>users/getusertest', { user: stuff }

but it's still a cpu hog.

cheers.[/quote]

This would indicate that it is not your javascript but your code on the server. I use jQuery all the time, and don't have any problems.
#6

[eluser]rickrude[/eluser]
Fair enough, but if I don't use a function in the controller, and just use a separate php file, it's all good. This is why I am posting here.

cheers
#7

[eluser]rickrude[/eluser]
If I tell it to output to the javascript console, I can see it's doing it multiple times - as in 100's of times, and each consecutive click is more than doubling the amount of console logs. I am definitely missing something here.

cheers.
#8

[eluser]rickrude[/eluser]
[SOLVED] I had the code wrapped in a mousemove() handler! why? I was messing around with mouseover events. Anyway, that explains the iphone working Smile




Theme © iAndrew 2016 - Forum software by © MyBB