Welcome Guest, Not a member yet? Register   Sign In
Where to place a JQuery file? [SOLVED]
#1

[eluser]Frychiko[/eluser]
Hi,

I've got jquery up and working but now I'm trying to test out some simple ajax/php:

Code:
$("tr.map_category").click(function() {

        var temp = $(this).attr("id");
        var variables = temp.split(" ");
        
        var id = variables[0];
        var category_id = variables[1];

        $.post("ajax_map_category.php", { id: "10" },
            function(xml) {
                alert("hey");
            }, "xml");
        
        alert("finished");
        
    });

The problem is... where do I place ajax_map_category.php?

It would be nice if I could place all my ajax files into the ajax folder below:

Code:
codeigniter/

- system

- public
     - img
     - js
     - css
     - ajax

- user_guide
#2

[eluser]richthegeek[/eluser]
jQuery + CI works quite well, but you need to be aware of a few things..

Because of CI's URL routing system (eg index.php/welcome/test) the browser will look for "ajax_map_category.php" at "index.php/wecome/test/ajax_map_category.php"

To negate this you need to either use a root symbol (eg "/ajax_map_category.php" and put it in the root folder of your site), or add a javascript global variable which contains the base_url() output.
#3

[eluser]Frychiko[/eluser]
Thanks for that advice, though it doesn't seem to have any effect :c

ajax_map_category.php
* located at the root of localhost/ci2/
Code:
<?=header("Content-type: text/xml")?>
<response>
    <id>56</id>
</response>


jquery part
Code:
$.ajax({
    type: "GET",
    URL: "http://localhost/ci2/ajax_map_category.php" ,
    success: function(xml) {                
        var id = $("response", xml).find("id").text();
        alert(id);
    }
});

id returns nothing and I don't think the php file is even being called..
#4

[eluser]richthegeek[/eluser]
the root will go for localhost/ajax_map_category.php

try using a line like this:
Code:
[removed]
var base_url = &lt;?=base_url();?&gt;
[removed]

then prepend all ajax urls with
Code:
base_url +
#5

[eluser]Frychiko[/eluser]
ok, I passed base_url() as a parameter to my jquery function.

base_url() = http://localhost/ci2/

Code:
var base_url = variables[2];
var script = base_url + "ajax_map_category.php";
        
    $.ajax({
        type: "GET",
        URL:  script,
        success: function(xml) {
                
            var id = $("response", xml).find("id").text();
                
            alert(id);
                
        }
    });

Not working either.. :o
This all works fine outside of CI (porting a website over to CI)
#6

[eluser]richthegeek[/eluser]
try alert'ing the script variable, make sure it is pointing to the correct place.

Finally, try getting the results of that page just as text - it might be a content-type error that is stopping jQ from parsing the result as XML
#7

[eluser]Frychiko[/eluser]
script variable outputs:
Code:
http://localhost/ci2/ajax_map_category.php

xml object returns source code of current page (wrong)

The content type is right.. I checked some previous ajax files from past projects.

I wrote to a log file in the php file, but there's no log file.. so I don't even think the file is being called..

Code:
&lt;?=header("Content-type: text/xml")?&gt;
&lt;?php
$file = "zebra.txt";
$handle = fopen($file, "w");
fwrite($handle, "smelly poo");
fclose($handle);
?&gt;
<response>
    <id>56</id>
</response>
#8

[eluser]SpooF[/eluser]
Your best bet is to call the exact URL structure your passing in your script directly in your browser. See if the page even exists. If your using any .htaccess to remove the index.php you will need to make an exception for that file other wise its calling

Quote:http://localhost/ci2/index.php/ajax_map_category.php
#9

[eluser]Frychiko[/eluser]
Oh, file definitely exists in the URL.

Anyway found the solution in my sleep last night.
the URL word in the parameter list was being capitalized.. should have been all lowercase.

Solved!




Theme © iAndrew 2016 - Forum software by © MyBB