how to change url for javascript in opencart - javascript

i have been working on the opencart frontend. And i want to make a frontend structure such that products uploaded by a particular vendor is shown. For that i use the url 'user/vendor_name'. And i have made changes in htaccess file for such url and after that i have changed the link function in url.php file for such cases. So now if a user clicks anywhere in the website the url will show 'user/vendor_name/index.php......'. But the url in javascript doesnt use $this->link function and those changes of url.php file doesnot take effect and thus it redirects to the original url.
Please help me out on this.

You would have to modify the controllers for each template containing such URLs and make sure You are setting a vendor name to a PHP variable accessible by the template:
$this->data['vendor'] = $vendor_information['name'];
supposing the vendor name is stored in a variable $vendor_name under the index name. This is only an example. Now in each template identify such URL with the JS part:
$.ajax({
url: 'index.php?route=checkout/cart/add' // + ...
// ...
});
and change it to:
$.ajax({
url: 'vendor/<?php echo $vendor; ?>/index.php?route=checkout/cart/add' // + ...
// ...
});
This should solve your problem.

Related

How to write path to files in Magento? File is not found, even when I set the absolute path

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.

How to download a file via URL then get its name

This is for a userscript I'm making with JS+jQuery. I'm wondering if it's possible to find the filename given the URL.
It's in the form of:
http://example.org/download.php?action=download&id=1234
and then that link downloads a file such as "cat.jpg".
How do I find out what the file name is called? I don't need to actually save the file on the users computer - just need to find the name of the file.
I'm open to using any JS library - but I need to make sure that the file isn't actually saved in the users computer (or maybe it's just saved in a temp folder somewhere).
The simple thing you can do is to make HEAD request, so that you don't actually download the file but only response headers. From there you get Content-Disposition header which contains filename field.
Something like this in jQuery:
$.ajax({
type: "HEAD",
url: 'http://example.org/download.php?action=download&id=1234',
success: function(message, text, response) {
var header = response.getResponseHeader('Content-Disposition');
console.log(header);
}
});
header variable will be something like attachment; filename="image.jpg". Now it's easy to extract filename part:
var filename = header.match(/filename="(.+)"/)[1]; // image.jpg

Javascript get real path in any situation ($.ajax)

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>

Relative URLs in javascript/jquery & rails 3

I have a URL like this for each record:
http://localhost:3000/items/3/stuff.json
http://localhost:3000/items/1/stuff.json
http://localhost:3000/items/4/stuff.json
http://localhost:3000/items/9/stuff.json
when on a page such as http://localhost:3000/items/3/, the linked JavaScript file has code like this:
$.getJSON(document.URL+'/stuff.json');
this allows me to grab the JSON file without worrying about the record ID number.
The problem arises on a URLs such as:
http://localhost:3000/items/3/news
http://localhost:3000/items/3/photos
as this:
$.getJSON(document.URL+'/stuff.json');
will be looking for:
http://localhost:3000/items/3/news/stuff.json
which of course, does not exist.
Any ideas for how I should solve this?
You can use
$.getJSON("../stuff.json", function(data){
});
if current url is "http://localhost:3000/items/3/anything"
EDIT:
$.getJSON(document.URL.match(/\/items\/\d+\//) + "stuff.json", function(data){
});
If all the urls are of the form http://host/items/id/anything
If this JS is rendered at all by Rails (i.e. it's not something that comes from the Asset Pipeline) then you can use the routing helpers, like this:
<script>$.getJSON("<%= item_news_stuff_path(3, :format => :json) %>")</script>
This would generate a relative-to-root path to the resource, building the route up correctly.

how not to hard-code codeigniter link in js file

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');
})

Categories

Resources