codeigniter - 窗体的动作属性 - 如何在javascript中指定完整的应用程序路径 [英] codeigniter - form's action property - how to specify full app path in javascript

查看:118
本文介绍了codeigniter - 窗体的动作属性 - 如何在javascript中指定完整的应用程序路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我看来有以下javascript:

  $ .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')。 port')。val()+'>< input type ='text'id ='newVlanID'style ='width:5em; height:1.5em'/>& nbsp;& nbsp; < / form>;
alert(submitFormHTML); button type ='submit'class ='btn'id ='saveVlan'style ='width:10em; height:2em'> ;
$('#clientajaxcontainer')。html(htmlstring);
$('#newvlanform')。html(submitFormHTML);

我目前为submitFormHTML字符串中的表单的action属性定义的路径是不正确。而不是让我的用户具有所有参数的http:// myserver / myapp / controllerX / methodABC /,它将controllerX / methodABC /附加到当前URL的结尾。
所以如果用户在:

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

当点击按钮提交表单时,他们最终到达:

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

有没有办法在javascript中获取site_url()或base_url?
任何建议?感谢您阅读帖子。



编辑:



根据某人的建议(我认为这是一个好主意)我创建了一个名为global.js的新的.js文件,我有一行:

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

这个文件包含在我的模板PHP文件中,我的视图如下:

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


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

我修改了我的javascript,使表单看起来像:
submitFormHTML =    Reassign Vlan;
alert(submitFormHTML);



Edit2:



有趣的是,当我定义BASEPATH global.js,我的javascript结束生成的URL看起来像这样:

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

正如你所看到的,而不是解释,它只是包括文本​​。



但是如果我忘记包含一个单独的js文件,只需这样做:



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

它工作正常。



我看不到为什么include文件失败。

解决方案

更新答案以匹配您的新问题



header.php

 < base href =<?php echo base_url();?> /> 

< link href =assets / css / bootstrap.css =stylesheet>

< script>
//全局vars,可以在任何外部js文件中使用
var BASEPATH =<?php echo base_url();?>;
var IP =<?php echo $ _SERVER ['REMOTE_ADDR'];
< / script>

< script src =assets / js / global.js>< / script>

global.js


$ b b

 (function($){

var formsObject = {
init:function(){
if form#id1))
{
this.formOne; //如果id存在则运行一个
}
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'),
//等等
});
}
}
} $ b b
// ready
$(function(){
formsObject.init();
});

})(jQuery);


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/" 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

<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);

这篇关于codeigniter - 窗体的动作属性 - 如何在javascript中指定完整的应用程序路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆