peacemaker
peacemaker

Reputation: 2591

Jquery in separate file using codeigniter & php

I understand that you can put your $(document).ready() code into separate files from the html, however as part of my javascript I use some calls back to codeigniter PHP functions such as site_url() which doesn't work when part of a separate .js file.

Here's an example:

login_page.php

<script type="text/javascript" src="<?php echo site_url('scripts') . '/' . 'app/login_page.js'; ?>"></script> 
<div id="loginForm" class="prepend-2 column span-21">
    <p class="tips"></p>
    <div class="column span-10">
        <dl>                
            <dt><label>Email:</label></dt>
            <dd><input type="text" name="email" id="email" class="text" /></dd>
        </dl>
    </div>
    <div class="column span-10 last">
        <dl>                
            <dt><label>Password:</label></dt>
            <dd><input type="password" name="password" id="password" class="text" /></dd>
        </dl>
    </div>
    <div class="column prepend-7 span-10 append-5 ">
        <dl>                
            <dt><label>&nbsp</label></dt>
            <dd><input type="submit" value="Login" id="loginButton" /></dd>
        </dl>
    </div>
</div>

login_page.js

$(document).ready( 
function()
{
    //login
    $("#password").keyup(function(event){ if(event.keyCode == 13){          $("#loginButton").click();}}); //submit on enter key
    $('#loginButton').click(function()
    {            
        do_login($("#email"), $("#password"), $(".tips"), "<?php echo site_url('dashboard'); ?>", "<?php echo site_url('login/log_in'); ?>");
        return false;
    });
});

So the problem here is in the do_login I'm trying to call PHP site_url but it obviously won't work. It all works ok if I have the javascript in my php inline but that makes the files rather large and I'd prefer separate file.

What I need to know is this even possible? If so, has anyone come up with an elegant solution to this problem?

Upvotes: 1

Views: 890

Answers (2)

Laurence
Laurence

Reputation: 60048

The easiest way is to define a JS variable in your HTML - which is used by your JS file.

<script type="text/javascript">  
    _Base_URL = "<?php echo base_url();?>";
</script>

Then in your JS

do_login($("#email"), $("#password"), $(".tips"), (_Base_URL+"dashboard"), (_Base_URL+"login/log_in) ");

Upvotes: 0

Prasenjit Kumar Nag
Prasenjit Kumar Nag

Reputation: 13461

You can define the base_url as a JS global variable or even better declare a JS global object with all url's you may need later in your login_page.php before including login_page.js. Then they will be in a separate container and will be available in any JS code loaded after that declaration, Inline or external.

<script type="text/javascript">  
    window.URLS  = {
      "BASE_URL" : "<?php echo site_url(....); ?>",
      "LOGIN_URL" : "<?php echo site_url(....); ?>",
      "DASHBOARD_URL" : "<?php echo site_url(....); ?>",
    };
</script>

Then use it in login_page.js like

do_login($("#email"), 
         $("#password"), 
          $(".tips"), 
          URLS.DASHBOARD_URL, 
          URLS.LOGIN_URL);

Upvotes: 1

Related Questions