I have an ajax function:
$.post("/ajax.php",{query:data},function(result){...
the location of ajax.php is in the root of my domain (i.e. site.com/ajax.php)
this same script gets called when the user is in a folder (i.e. site.com/m/)
The problem I face is that I want to reference the root directory. Usually /file.php refers to the home directory as opposed to file.php.
My requests are being sent to http://site.com/m/ajax.php.
Does anyone know what I should do?
I ended up doing as #Ohgodwhy suggested:
$.post(
"<?php echo $_SERVER['HTTP_HOST'];?>`/ajax.php",
{query:data},
function(result){
// alert(result);
});
So far, it's working for me!
Related
everyone!
I try to create custom application sending (to emails) form the site on Magento.
For doing it, I call post.php file in this way:
$('form#send-profile').submit(function(event) {
event.preventDefault();
var output = true,
array = $(this).serialize();
if (output) {
$.post('post.php', array, function(data) {
$('input[type="text"], textarea').val('');
});
};
});
I`ve placed post.php into the root folder (and tried it with different folders too).
But I had this result in any conditions:
POST http://vescorte.com/post.php 404 (Not Found)
Maybe Magento has some special way to set paths? Tell me please, if you know how to cope with this problem.
First of all , which version of magento do you use ?
Next, if you want to development any new function , you need create a module
try this link magento-2-module-development
In the template file in which you mentioned JS code, you can mention the Magento base URL function to get full base URL like below:
$.post('<?php echo Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB); ?>post.php', array, function(data) {
$('input[type="text"], textarea').val('');
});
Still if you face issue then try to remove all the code from post.php and run with simple text to check file request complete or not.
I have a php file (I will call ORIGINAL) which do some calculations (through db mysql). I want to read this php from javascript. For that operation I have used ajax function and my php uses echo $result to print the data I need.
Everything is perfect here.
What happends now, I am creating another php file which need to call the ORIGINAL php file. If I want to call it, I must change the echo to return which is normal. This causes that my javascript call doesnt work.
Do you have a solution which work for both situations?
Thanks in advance.
Do you mean something like this?
original_php_file.php:
<?php
require_once "other_php_file.php"; // include all of the other files contents
// all code contained within original_php_file
?>
You were being pretty broad with your request (not including file names or code), so this is all I can assume you need.
Tell me if it helps :-)
Just send one more parameter into your ajax request to tell that ORIGINAL php file what type of output it should return.
Into your ORIGINAL file check for that output so you can understand from where that request come and what output you should return.
$.ajax({
url: 'ORIGINAL.php',
data: 'data=test&output=1',
success: function(r){
// here you have your output
}
});
My project has the following structure:
Sil
-> css
-> js
-> clients
-> index.php
-> response.php
index.php
My problem is on the folder clients.
My url can have multiple choices, some of them are:
http://localhost/Sil/clients/
http://localhost/Sil/clients/all/
http://localhost/Sil/clients/stackoverflow/2
http://localhost/Sil/clients/stackoverflow/2/technology/5
As you can see I use .htaccess to write rules.
That said, the problem here is with the url inside the $.ajax.. I use the following code inside the clients/index.php file.
$.ajax({
url : 'response.php?type=add',
});
This code works perfectly if and only if my url is http://localhost/Sil/clients/.
In case the url are the other options said above, it won't work, because the file URL output will be:
http://localhost/Sil/clients/all/response.php?type=add
Which is clearly wrong.
I have already solved this issue by using a complete url, but I do not believe this is the best approach since when I have to upload the files to the server by FTP I have to change in every $.ajax from localhost to my domain name.
$.ajax({
url : 'http://localhost/Sil/response.php?type=add',
});
What other options do I have? I've tried, without success, the window.location.pathname.
You can either start the URL with a slash in order to specify the "root directory":
$.ajax({
url : '/Sil/clients/response.php?type=add',
});
or use a <base> tag in HTML:
<base href="http://localhost/Sil/clients/" />
<script>
$.ajax({
url : 'response.php?type=add',
});
</script>
i wrote the routine below to retrieve city based on selected country for my codeigniter application.
$(document).ready(function() {
$("select#cbo_country").change(function() {
$.post("http://localhost/main/index.php/city/get_data_by_country", {
int_country_id : $(this).val()
},
function(data) {
// some code here
},'json');
})
});
as you can see, i hard-coded the url (http://localhost/main/index.php/city/get_data_by_country) and i know it's a bad practice but i can't help it.
is there a nice clean way to not hard-code the url? i used to use codeigniter's base_url(), but since i move the routine to a js file, i am no longer able to use the function.
Taken (mostly) from my answer on How to get relative path in Javascript?.
You've got two options:
Build a configuration/ preferences object in JavaScript which contains all your environment specific settings:
var config = {
base: "<?php echo base_url(); ?>",
someOtherPref: 4
};
and then prefix the AJAX url with config.base.
You have to place the config object in a place which is parsed by PHP; the standard choice is inside the <head> HTML element. Don't worry its a small config object, whose contents can change on each page, so it's perfectly warrented to stick it in there.
Use the <base /> HTML tag to set the URL prefix for all relative URL's. This affects all relative URL's: image's, links etc.
Personally, I'd go for option 1. You'll most likely find that config object coming in handy elsewhere.
Change "http://localhost/main/index.php/city/get_data_by_country" to "/main/index.php/city/get_data_by_country" and it will work no matter what your base url is.
This works because the / before main/index.php says "start at the root and go the the index.php file in the main folder".
This is unless your document root folder is set to the main folder, if so, take out the main
One thing you can do is add a data-baseurl attribute to an element on the page (the body element works). Then you can grab that from the JavaScript file.
<body data-baseurl="<?=base_url()?>">
Then in JavaScript:
$("select#cbo_country").change(function() {
var baseURL = $('body').data('baseurl')
$.post(baseURL+"city/get_data_by_country", {
int_country_id : $(this).val()
},
function(data) {
// some code here
},'json');
})
Fellow Coders,
so far I have been using the CI url helper to build full urls using base_url() and site_url(). Now I'm trying to access a controller function within some javascript code that will be loaded as a js file.
it's actually an ajax call with a url parameter that should be something like:
url : '/account/check_user'
now unless i prefix the url with the full path as ins http://servername/..... the code fails.
all the code examples i've seen use the short version of the url but i cannot get it to work. I'm sure this is really simple but i'm stuck.
i could pass a hidden form field to the js code but i'd rather not. any ideas?
thanks
Well, I also tend to use absolute URLs and a good practice I always do is declaring a JS variable:
var base_url = "<?php echo base_url(); ?>";
In:
the head section
as the first line of my script tag
if I have a main.js file that holds most of my JS code AND it's always included in my views, then I put that line first thing in the file.
After that, you use it like:
url : base_url + 'account/check_user'
Anyway, the first slash / in your url tells the browser to go to the URL root which would not be the right place to put your url chunk in! for example:
if your CI installation is in ci folder and your URL is: domain.com/ci/contorller/method/
Then your URL will become: domain.com/contorller/method/!!
I'm usually assigning the base_url() to a JS variable right in the head to have it available to all methods. Something like this
<script type="text/javascript">
var baseUrl = "<?php echo base_url() ?>";
</script>
You should actually be using CI's site_url() function. base_url() is useful for generating a URL to a resource (such as script or stylesheet), but site_url() is the best choice when generating a URL to a page within the app itself, such as when making an Ajax request, as described.
So the best code to use would be something along these lines:
<script type="text/javascript">
var site_url = "<?php echo site_url() ?>";
</script>