title: codeigniter - form's action property - how to specify full app path in javascript
body: <p>i have the following javascript in my view: </p>
  
  <pre><code>  $.ajax({
      url:"&lt;?php echo site_url('switches/showknownvlans/'.$ip.'/'.$hardwaremodel);?&gt;",
      type:'POST',
      dataType:'json',
      success: function(returnDataFromController) {
      //alert(returnDataFromController.length);
      var htmlstring;
      var submitFormHTML;
      htmlstring = "some html stuff";
  
      for(i = 0; i &lt; returnDataFromController.length; i++) {
  
      }
      submitFormHTML = "&lt;form method='post' accept-charset='utf-8' action='controllerX/methodABC/"+ $('#ip').val() +"/" + $('#hardwaremodel').val() +"/" + $('#port').val() + "'&gt;&lt;input type='text' id='newVlanID' style='width:5em;height:1.5em'/&gt;&amp;nbsp;&amp;nbsp;&lt;button type='submit' class='btn' id='saveVlan' style='width:10em;height:2em'&gt;Reassign Vlan&lt;/button&gt;&lt;/form&gt;";
      alert(submitFormHTML);
      $('#clientajaxcontainer').html(htmlstring);
      $('#newvlanform').html(submitFormHTML);
  </code></pre>
  
  <p>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/" with all the parameters, its appending "controllerX/methodABC/" to the end of the current URL. 
  so if the user is at:</p>
  
  <pre><code> http://myserver/mypp/controller23/method123/
  </code></pre>
  
  <p>when the click on the button to submit the form, they end up at: </p>
  
  <pre><code> http://myserver/mypp/controller23/method123/controllerX/methodABC/
  </code></pre>
  
  <p>Is there a way to get either the site_url() or base_url in javascript?
  Any suggestions?  Thanks for reading the post.</p>
  
  <p>Edit: </p>
  
  <p>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: </p>
  
  <pre><code>var BASEPATH = "&lt;?php echo base_url(); ?&gt;";
  </code></pre>
  
  <p>This file is then included in my template PHP file for my view like so: </p>
  
  <pre><code>&lt;!DOCTYPE html&gt; &lt;!-- aka HTML5 --&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
  
  
  &lt;link href="&lt;?php echo base_url();?&gt;assets/css/bootstrap.css" rel="stylesheet"&gt;
      &lt;script type='text/javascript' src='&lt;?php echo base_url();?&gt;assets/js/global.js' charset="utf-8"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>I've modified my javascript that craetes the form to look like: 
          submitFormHTML = "&nbsp;&nbsp;Reassign Vlan";
          alert(submitFormHTML);</p>
  
  <p>Edit2: </p>
  
  <p>What's interesting is that when I define BASEPATH in the global.js, the URL my javascript ends up generating looks like this: </p>
  
  <pre><code> http://myserver/myapp/index.php/switches/showportvlan/parm1/parm2/%3C?php%20echo%20base_url%28%29;?%3E/index.php/switches/changeportvlan/parm1/parm2/parm3. 
  </code></pre>
  
  <p>As you can see, instead of interpretting the "", it just included the text as is. </p>
  
  <p>But if i forget including a separate js file and just do this: 
        
       
       
        </p>
  
  <pre><code>&lt;link href="&lt;?php echo base_url();?&gt;assets/css/bootstrap.css" rel="stylesheet"&gt;
      &lt;script type='text/javascript'&gt;
           var BASEPATH="&lt;?php echo base_url();?&gt;";
       &lt;/script&gt;
  </code></pre>
  
  <p>it works just fine. </p>
  
  <p>I can't see why the include file fails.</p>