doujiazong0322 2010-06-17 19:30
浏览 46
已采纳

来自外部源的变量名称中的点和空格将转换为下划线

Trying a bit of AJAX, and I find that much of my data is littered with underscores! Documentation confirms that this is working as intended. Any way to pass my form information to PHP intact? I'm using CodeIgniter, so my pass looks like /controller/function/variable, receiving controller:

controller{
 function($v=0){#what once was hello world is now hello_world...}
}

I can't very well do an undo, data might contain an underscore.

Thanks, Brandon

Edit:

I think it is converting the value. Here is the gist of the code:

<form>
<text input name="tbox"/>
<submit/>
</form>

 ajax_handler(
    v = form.name() + form.val()
    do_ajax('/controller/function/v')
 )

controller(){
   function($v=0){#spaces and periods in v are converted to underscore}
} 

thanks again, brandon

here is the actual code:

<input type="text" id="tusername" name="tusername" class="checkable tbox"/>
<button id="unsubmit" name="wizard" class="formable">next</button>


        $('.formable').live('click',function(event){
            event.preventDefault();
            var n = $(this).attr('id');
            var a = $(this).attr('name');
            var v = dosend(); 
            $.ajax({
                    url: '/form/'+n+'/'+v,
                    type: 'post',
                    success: function(result){
                         alert(result);
                    }  
            });
            function dosend(){
                    var inputs = $(":input");
                    var s = "";
                    inputs.each(function(){
                            s += $(this).attr('name')+":";
                            s += $(this).val()+";";
                    });
                    return s;
            }
    });

   class Form extends Controller{
       function Form(){
           parent::Controller();
           session_start();
       }
       function unsubmit($v=6){
           print $v;
       }
   }

anything in the string that gets passed to the controller's function that is a space or period gets converted to underscore. I type hello world into this box, and it prints out hello_world.

            $w = explode(';',$v);
            foreach($w as $i){
                    $x = explode(':',$i);
                    if(isset($x[1])){
                      $_AJAX[$x[0]] = $x[1];
                    }
            }
  • 写回答

4条回答 默认 最新

  • douyu0725 2010-06-17 20:05
    关注

    If you need them preserved, considering sending as something else then application/x-www-form-urlencoded or multipart/form-data. For instance, this nice questioneer tried to use JSON, which you can parse at your leisure: handle json request in PHP

    JSON would be easier to parse. You can however use the application/x-www-form-urlencoded form (which wouldn't handle file-uploads), file_get_contents('php://input'), and parse the string to your liking yourself.

    With a dirty, dirty hack, I was delighted to be told just last week this can also work for multipart/form-data: Get raw post data


    Missed the "'m just using post, separating keys and values with ':' and pairs with ';' " apparantly:

            $.ajax({
                    url: '/form/'+n+'/'+v,
                    type: 'post',
                    contentType: 'text/plain', //<-- add this
    

    Possibly parse by this:

        <?php
        $post = file_get_contents('php://input');
        $pairs = explode(':',$post);
        $values = array();
        foreach($pairs as $pair){
             $vars = explode(':',$pair,2);
             $values[$vars[0]] = $vars[1];
        }
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了