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'/> <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/” 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.
Updated Answer to match your new question
header.php
global.js