Welcome Guest, Not a member yet? Register   Sign In
Can't post with JQuery
#1

[eluser]Razican[/eluser]
Hello, I have a code in JQuery to do a $.post but I can't get the post information in my controller. When I set overall XSS to TRUE, it does 5 "XSS filtering completed" and it stops there, and when I set it to FALSE, it stops after loading the input class. Here is my JQuery code:

Code:
$(function(){
    $('input').bind('blur keyup', function()
    {
        $('#form_result').html(loading);
        var name    = $(this).attr('name'),
            value    = $(this).val();
        $.post("can't put links", { name: value }, function(data) {
            alert(data);
        });
    });
});

Obviously I don't receive the alert.

Here is my controller:
Code:
public function request()
    {
        if ($this->input->is_ajax_request())
        {
            $this->lang->load('registration');
            $this->load->model('registration_m');

            $name    = $this->input->post('name');
            log_message('debug', 'Name: "'.$name.'"');

            switch ($name)
            {
                case 'username':
                    echo 'OK';
                break;
                default:
            }
        }
        else
        {
            log_message('error', 'User with IP '.$this->input->ip_address().' has tried to enter without doing an AJAX request.');
            redirect('registration');
        }
    }
}

If you need more information, please, let me know.
#2

[eluser]Aken[/eluser]
Use Firebug or some other Javascript developer toolbox to see what kind of requests and responses are being made. Once you know where the code is stopping, you can adjust it accordingly.

First, do you actually have the Javascript variable 'loading' defined before running this line of code?:
Code:
$('#form_result').html(loading);

If not, that is a potential issue. There's nothing wrong with your $.post Ajax, except that even though you're defining the variable 'name', when you put that into the $.post data, it gets taken literally as the word 'name', not the variable value as defined earlier. I'd suggest not defining name and value, and replacing the data object with $(this).serialize()
#3

[eluser]Razican[/eluser]
I had the loading variable defined. And I changed the { name:value } with $(this).serialize() but I continue getting this in the log of CodeIgniter:

Code:
DEBUG - 26-04-2011 11:44:37 --> Config Class Initialized
DEBUG - 26-04-2011 11:44:37 --> Hooks Class Initialized
DEBUG - 26-04-2011 11:44:37 --> Utf8 Class Initialized
DEBUG - 26-04-2011 11:44:37 --> UTF-8 Support Enabled
DEBUG - 26-04-2011 11:44:37 --> URI Class Initialized
DEBUG - 26-04-2011 11:44:37 --> Router Class Initialized
DEBUG - 26-04-2011 11:44:37 --> Output Class Initialized
DEBUG - 26-04-2011 11:44:37 --> Security Class Initialized
DEBUG - 26-04-2011 11:44:37 --> Input Class Initialized
DEBUG - 26-04-2011 11:44:37 --> XSS Filtering completed
DEBUG - 26-04-2011 11:44:37 --> XSS Filtering completed
DEBUG - 26-04-2011 11:44:37 --> XSS Filtering completed
DEBUG - 26-04-2011 11:44:37 --> XSS Filtering completed

I would like to use FireBug, but I only know how to use it's HTML and CSS functions.
#4

[eluser]pickupman[/eluser]
In Firebug, click on the Console tab. This is probably where you are seeing your log messages already. When the blur or keyup is fired, you should see in your console some like
+POST '/yoururl/controller/method/' 200 OK

If you click on the + beside POST you should be able to see what variables where posted, and what was returned by the server under the Response tab.

Your code looks correct.

Try placing an alert before your $.post to see if the event is being fired. Silly to ask, but are you linking to the proper jQuery source?
#5

[eluser]arnonymous[/eluser]
I'm actually having some similar problems now.
And i think a lot of problems comes from the linking.
Somehow i can't post using absolute links and i need to post with only "controller_function/" instead of "controller_name/controller_function/". I don't get the logic of that. place some alerts or echo's in the controller function and in the JS function to see if they all get called.
#6

[eluser]pickupman[/eluser]
I recommend the syntax:
Code:
$.post('<?php echo site_url('controller/method');?>',{names: values}, function(data){});
#7

[eluser]arnonymous[/eluser]
that's exactly what i was thinking. but when i try that it doesn't work
it only seems to work with:
Code:
$.post('<?php echo site_url('method');?>',{names: values}, function(data){});
But even this has started failing for me that's why im here, any ideas?
#8

[eluser]pickupman[/eluser]
Without specifying the controller or a route, having just the method will not work. Got a link you could PM?
#9

[eluser]Sudz[/eluser]
[quote author="pickupman" date="1303843729"]I recommend the syntax:
Code:
$.post('<?php echo site_url('controller/method');?>',{names: values}, function(data){});
[/quote]



how can i use this in .js file.
Please if someone has solution.
#10

[eluser]Razican[/eluser]
[quote author="arnonymous" date="1303932357"]that's exactly what i was thinking. but when i try that it doesn't work
it only seems to work with:
Code:
$.post('<?php echo site_url('method');?>',{names: values}, function(data){});
But even this has started failing for me that's why im here, any ideas?[/quote]

I had been using it.

@Sudhakar Prajapati:

I just created head_ajax.php view with all th jquery in it, and then, did $this->load->view('head_ajax').




Theme © iAndrew 2016 - Forum software by © MyBB