Partial url in CodeIgniter - javascript

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>

Related

How to use base url or php variable in javascript file

I am new in PHP, i want to use "base_url"(php variable) in javascript file,But right now i am unable to load image,Here is my current code,How can i do this ? Thanks in advance.
$('#site-logo').find('img').attr( {src:'<?php echo base_url(); ?>assets/images/logo/logo_dark#2x.png',width:'151',height:'45'} );
You can create a variable or a constant in the javascript and store the base url there and use the same in the code.
Something along the line of following
var BASE_URL = "<?php echo base_url(); ?>";
$('#site-logo').find('img').attr({src: BASE_URL+'/assets/images/logo/logo_dark#2x.png',width:'151',height:'45'});

How to read global variable from WP-Config.php in JS file

I have defined URL in wp-config file.
DEFINE('URL', 'google.com');
Now i want to access this URL from my JS theme file:
<script> alert(URL); </script>
How to do this?
use localize in functions.php
add_action('wp_enqueue_scripts' , function(){
wp_localize_script('jquery', 'config_var', URL );
});
and in js file => config_var will equal the config variable value
You have to put a bit of your JS in your PHP file (i.e. use script tags to accomplish this). Then do the following:
Place the script in your php file
<script> alert(<?php echo URL ?>); </script>
you can read the php variable inside th js using echo. use like this
var url='<?php echo _URL; ?>';
alert(url);

Echo php content into API javascript?

I would need to echo php variables (from the server and related to service behaviour) into javascript. For example, "ssl" => "true" and adapt accordingly in javascript. The thing is I'm doing this for some API files I'm writing and I want the client to have direct access to these files (<script src="... .js">) and this forces me to hardcode info that might change in the future across multiple file references. What I'd like to do was something like this, is it possible? (the content to fetch must remain completely private - from the server folders to the php script files - and so it is really not an option to fetch this info using javascript):
api.js
<? //PHP here. (I know...)
(...)
?>
//some javascript
var is_secure = <? echo "true"/"false" ?>
(...)
So that when doing <script src="api.js"/> the user only fetches:
var is_secure = "true";
(...)
or
var is_secure = "false";
(...)
Any idea on how I could do something similar? Thank you very much...
You could just create your PHP file with this line before everything :
header("Content-Type: application/javascript");
EDIT:
I did misunderstood the question. If you want js in an php file, you should do it like this:
header("Content-Type: application/javascript");
OLD ANSWER:
I don't know if you know, but client can't see your php code...
So if You'd:
echo "Something"
The client would only see Something.
It's not clear, what you're trying to do...
If you don't want to see Something when you go directly into your "secret" php page, then you should do it like this:
JS:
jQuery.ajax({
type: "POST",
url: 'secretfile.php',
dataType: 'html',
data: {apiRequest: 1},
success: function (response) {
yourVar = response;
}
});
PHP:
If ($_POST['apiRequest']==1){
echo "Something";
}
yourVar would be = "Something"
If you'd go to this page, it wouldn't display anything;

CakePHP - Handling urls in ajax calls

What is the better way to deal with urls in ajax calls made in javascript files present in the webroot and because of that, are not interpreted by PHP?
I'm using CakePHP and require.js and therefore would not put the javascript code directly in views. The only way I found was to declare a variable in the layout that receives the value of the webroot like this:
<script>var webroot = "<?php echo this->Html->url('/') ?>" </script>
And then in my js files I hardcoded the urls to the ajax calls like this:
$.getJSON(webroot + 'users/list', function(){ ... } );
But it does not solve the problems if there are changes in the Routes file. I generally change the routes to be more friendly after I finished the project and this would cause a big problem if I have many ajax calls or urls been referenced in js files.
I usually work this way:
In my layout header I add the following before any other javascript is included:
<script type="text/javascript">var baseUrl = '<?php echo $this->base; ?>';</script>
Then at my javascript files I do this:
$.post("http://"+ document.domain + baseUrl +"/controller/action.json");
use
echo Router::url(array('controller' => 'Users', 'action' => 'list'));
Will output;
/Users/list
in js
$.post({url : "<?php echo Router::url(array('controller' => 'Users', 'action' => 'list')); ?>"})
I think you’re working against CakePHP’s conventions if you’re wanting to display your app’s data in JSON format. Have a look at the CakePHP cookbook entry on JSON and XML views.

Zend passing server url / parameter to javascript file

For my application I need to pass a parameter through zend to my exter javascript file.
I need the server url in the javascript with the language parameter.
my javascript is placed in /js/javascript.js
I've tried the follow
layout.phtml
$this->headScript()
->prependScript('BASE_URL = "test";')
->appendFile('/js/javascript.js');
javascript.js
var url = BASE_URL
But my firebug console keeps saying that BASE_URL is not defined. What
is the good way to do this?
Regards.
Nicky
First you include javascript.js, and just after it you prepend script before appended javascript.js.
$this->headScript()
->appendFile('/js/javascript.js')
->prependScript('BASE_URL = "test";');

Categories

Resources