Welcome Guest, Not a member yet? Register   Sign In
AJAX request path
#1

[eluser]ptvw2011[/eluser]
I am using some AJAX in my Code Igniter application but it only seems to work when the PHP file which is carrying out some processing is referred to absolutely.

I want to use a relative path as I don't want to change individual URL s when migrating from my local server.

Code:
new Ajax.Request("http://localhost/pth/to/my/php/file",
        {
          method: 'post',
          postBody: 'name='+ $F('name'),
          onComplete: showResponse
        });
}

At present I have the PHP file saved in a folder located in the Code Igniter root which is the same place as this JavaScript file.

Where is the best place to save the PHP file and how would I refer to it relatively in Code Igniter?

I've tried using '/filename.php' which I suppose should work as its in the same directory as this JavaScript file.

Thanks

me
#2

[eluser]carbona[/eluser]
You should user base_url() function and when migrating you will need only to change you config['base_url'] path
#3

[eluser]ptvw2011[/eluser]
Hi thanks for replying. as far as im aware you cant use base_url in a javascript function. I want to be able to use the native CI database class with my php file so I need a way to use a helper function and reference it in this ajax request also.
#4

[eluser]carbona[/eluser]
Well i was talking about the whole application when i wrote about base_url() function.

You could do this another way. Create a controller and a method for you ajax calls.

Create a javascript file named config.js where you will have something like this :

var base_url = [removed].protocol+"//"+[removed].hostname+"/";

Include this js on top of all others js files in head.

When you will move your applications everything will be working fine, and you dont need any other changes.

When making ajax calls :
Code:
new Ajax.Request(base_url+"yourcontroller/yourmethod",
        {
          method: 'post',
          postBody: 'name='+ $F('name'),
          onComplete: showResponse
        });
}
#5

[eluser]carbona[/eluser]
i forgot the code snippet :
Code:
var base_url = [removed].protocol+"//"+[removed].hostname+"/";
#6

[eluser]ptvw2011[/eluser]
ok thanks for your help i will give it a try
#7

[eluser]ptvw2011[/eluser]
I tried this implementation but using

Code:
function sendRequest() {
    new Ajax.Request(base_url + "test/deactivate/",
        {
            method: 'post',
            postBody: 'name='+ $F('da'),
            onComplete: showResponse
        });
}

It seems that the data never gets posted to the deactivate method/function within the test controller.

I have tried printing the post array on the resulting page but its always blank.

I have tested by just writing a simple controller and function to echo out a test message.

The test only works if i make a .php file in a folder and echo out.
#8

[eluser]InsiteFX[/eluser]
In your html head section add this.
NOTE: replace $ in $cript to Script...
Code:
<$cript type="text/javascript">
    //&lt;![CDATA[
        var base_url = '&lt;?php echo base_url();?&gt;';
    //]]>
</$cript>
Now you can use base_url in your scripts.

InsiteFX
#9

[eluser]ptvw2011[/eluser]
Thanks but I have this working already.

My issue is with the code I posted. The value doesn't go into the $_POST array from the Ajax Request object. It only works if I specify the absolute path to a php file but it doesnt work using MVC.
#10

[eluser]ptvw2011[/eluser]
Code:
function sendRequest() {
    new Ajax.Request(base_url + "test/deactivate/",
        {
            method: 'post',
            postBody: 'name='+ $F('da'),
            onComplete: showResponse
        });
}

can anyone see anything wrong this?

I have tried using the MVC approach and printing out the $_POST array in the deactivate method but unsuccessfully.

Any help would be greatly appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB