Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8497223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:04:25+00:00 2026-06-11T00:04:25+00:00

i have the following javascript in my view: $.ajax({ url:<?php echo site_url(‘switches/showknownvlans/’.$ip.’/’.$hardwaremodel);?>, type:’POST’, dataType:’json’,

  • 0

i have the following javascript in my view:

  $.ajax({
    url:"<?php echo site_url('switches/showknownvlans/'.$ip.'/'.$hardwaremodel);?>",
    type:'POST',
    dataType:'json',
    success: function(returnDataFromController) {
    //alert(returnDataFromController.length);
    var htmlstring;
    var submitFormHTML;
    htmlstring = "some html stuff";

    for(i = 0; i < returnDataFromController.length; i++) {

    }
    submitFormHTML = "<form method='post' accept-charset='utf-8' action='controllerX/methodABC/"+ $('#ip').val() +"/" + $('#hardwaremodel').val() +"/" + $('#port').val() + "'><input type='text' id='newVlanID' style='width:5em;height:1.5em'/>&nbsp;&nbsp;<button type='submit' class='btn' id='saveVlan' style='width:10em;height:2em'>Reassign Vlan</button></form>";
    alert(submitFormHTML);
    $('#clientajaxcontainer').html(htmlstring);
    $('#newvlanform').html(submitFormHTML);

The path that I currently have defined for my form’s “action” property in the “submitFormHTML” string is incorrect. Instead of taking my user to “http://myserver/myapp/controllerX/methodABC/&#8221; with all the parameters, its appending “controllerX/methodABC/” to the end of the current URL.
so if the user is at:

 http://myserver/mypp/controller23/method123/

when the click on the button to submit the form, they end up at:

 http://myserver/mypp/controller23/method123/controllerX/methodABC/

Is there a way to get either the site_url() or base_url in javascript?
Any suggestions? Thanks for reading the post.

Edit:

As per someone’s suggestion (I think it’s a good idea) I’ve create a new .js file called “global.js” and I have one line in it:

var BASEPATH = "<?php echo base_url(); ?>";

This file is then included in my template PHP file for my view like so:

<!DOCTYPE html> <!-- aka HTML5 -->
<html lang="en">
<head>
  <meta charset="utf-8">


<link href="<?php echo base_url();?>assets/css/bootstrap.css" rel="stylesheet">
    <script type='text/javascript' src='<?php echo base_url();?>assets/js/global.js' charset="utf-8"></script>

I’ve modified my javascript that craetes the form to look like:
submitFormHTML = ”  Reassign Vlan”;
alert(submitFormHTML);

Edit2:

What’s interesting is that when I define BASEPATH in the global.js, the URL my javascript ends up generating looks like this:

 http://myserver/myapp/index.php/switches/showportvlan/parm1/parm2/%3C?php%20echo%20base_url%28%29;?%3E/index.php/switches/changeportvlan/parm1/parm2/parm3. 

As you can see, instead of interpretting the “”, it just included the text as is.

But if i forget including a separate js file and just do this:

<link href="<?php echo base_url();?>assets/css/bootstrap.css" rel="stylesheet">
    <script type='text/javascript'>
         var BASEPATH="<?php echo base_url();?>";
     </script>

it works just fine.

I can’t see why the include file fails.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T00:04:26+00:00Added an answer on June 11, 2026 at 12:04 am

    Updated Answer to match your new question

    header.php

    <base href="<?php echo base_url(); ?>" />
    
    <link href="assets/css/bootstrap.css" rel="stylesheet">
    
    <script>
     //global vars, can be used in any external js file
     var BASEPATH = "<?php echo base_url(); ?>";
     var IP       = "<?php echo $_SERVER['REMOTE_ADDR']";
    </script>
    
    <script src="assets/js/global.js"></script>
    

    global.js

    (function($){
    
       var formsObject = {
           init : function(){
               if($("form#id1"))
               {
                  this.formOne; //run form one if id is present
               }
           },
           formOne : function(){
               var fOne = $("form#id1");
    
               fOne.submit(function(){
                   var data = {
                      '' : '',
                   }
    
                   do_ajax(data);
               });
    
               var do_ajax = function(data){
                    $.ajax({
                         url : BASEPATH + fOne.attr('action'),
                         //and so on
                    });
               }
           }
       }
    
       //ready
       $(function(){
           formsObject.init();
       });
    
    })(jQuery);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Javascript libraries loaded for my page. <script type=text/javascript src=http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js></script> <script
i have the following Jquery function inside my view:- <script type=text/javascript> $(document).ready(function () {
In my javascript code I have the following: function SelectTopics() { $.ajax({ url: /Administration/Loc/SelectTopics,
I have a following javascript that I call from my _form.html.erb. $.ajax({ type: GET,
I have the following jquery code in my view <script type=text/javascript> $(document).ready(function () {
Hi people I have the following JavaScript code in my Show All Games View:
i have the following javascript to post a form through ajax without refreshing the
I have the following form in a partial view @model PartDetail @using (Ajax.BeginForm(Create, Part,
I have the following code in my view: <% using (Ajax.BeginForm(JsonCreate, new AjaxOptions {
I'm following the tutorial here: http://www.jotorres.com/2012/01/using-jquery-and-ajax-with-codeigniter/ For my view, I have: <script type=text/javascript src=<?php

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.