Welcome Guest, Not a member yet? Register   Sign In
jquery + codeigniter problem
#1

[eluser]obay[/eluser]
Hi all,

I'm trying to use the
Code:
$.post()
of JQuery but it's not retrieving the values. This used to work when I didn't use CI.. (this is my first time using MVC for PHP). I suspect that it's because of the MVC structure that is causing the problem. Can you take a while to look at my code?

my controller:
Code:
function bug_add($review_id) {
    echo '<?xml version="1.0" encoding="UTF-8" ?>';
    echo '<response>';
        echo '<bug>data</bug>';
    echo '</response>';
}

my view:
Code:
$("#button_add").click(function(e) {
    $.post(
        "review/bug_add/&lt;?=$review_id?&gt;",
        {
            //some values here
        },
        function(xml){
            $(xml).find("bug").each(function() {
                alert( $(this).text() ); //this never gets called
            });
        },
        "xml"
    );
});

If I remove the "xml" from the $.post(), the data are retrieved.

What do you think is wrong?

Thanks for your time Smile
#2

[eluser]jdfwarrior[/eluser]
[quote author="obay" date="1253026985"]Hi all,

I'm trying to use the
Code:
$.post()
of JQuery but it's not retrieving the values. This used to work when I didn't use CI.. (this is my first time using MVC for PHP). I suspect that it's because of the MVC structure that is causing the problem. Can you take a while to look at my code?

my controller:
Code:
function bug_add($review_id) {
    echo '&lt;?xml version="1.0" encoding="UTF-8" ?&gt;';
    echo '<response>';
        echo '<bug>data</bug>';
    echo '</response>';
}

my view:
Code:
$("#button_add").click(function(e) {
    $.post(
        "review/bug_add/&lt;?=$review_id?&gt;",
        {
            //some values here
        },
        function(xml){
            $(xml).find("bug").each(function() {
                alert( $(this).text() ); //this never gets called
            });
        },
        "xml"
    );
});

If I remove the "xml" from the $.post(), the data are retrieved.

What do you think is wrong?

Thanks for your time Smile[/quote]

where you start with "function(xml)" is the callback function for $.post. Which is, when the process has completed, its sent the data, and got a response, do "this". The argument passed into the callback function is the data returned. You are then trying to select that data using $(xml), which is wrong. You need to select an object, in your case, it appears you should have:

Code:
$("#button_add").click(function(e) {
    $.post(
        "review/bug_add/&lt;?=$review_id?&gt;",
        {
            //some values here
        },
        function(xml){
            $("xml").find("bug").each(function() {
                alert( $(this).text() ); //this never gets called
            });
        },
        "xml"
    );
});

The difference being the "'s around the xml, just before the find. You need the "'s to signify that you are selecting an element.

Or were you referring to removing the "xml" data type at the bottom?
#3

[eluser]obay[/eluser]
I wanted to read the returned xml file
#4

[eluser]Nick Husher[/eluser]
It may be that jQuery requires well-formed XML documents, and you don't have a closing XML tag in your controller.




Theme © iAndrew 2016 - Forum software by © MyBB