2

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];
                    }
            }
Brandon Frohbieter
  • 17,563
  • 3
  • 40
  • 62

4 Answers4

1

Found mention of this at http://sholsinger.com/archive/2009/04/passing-email-addresses-in-urls-with-codeigniter/, and is now resolved.

Periods in values that are passed via URI segment can be improperly converted to underscores under specific conditions. For this to happen, you must be using mod_rewrite and also your RewriteRule directive passes the rewritten segments through the query string. Example:

RewriteRule ^(.*)$ /index.php?/$1 [L]

To fix the problem you must edit the configuration value uri_protocol. The default value is 'AUTO'. It must be set to 'QUERY_STRING'. Example:

$config['uri_protocol'] = 'QUERY_STRING';
Brandon Frohbieter
  • 17,563
  • 3
  • 40
  • 62
  • I upvoted your answer, however it would be good to know what if any collateral damage can be caused by switching from `AUTO` to `QUERY_STRING` – Madbreaks Jul 09 '12 at 19:51
0

As far as I'm aware, no.

Like you said, PHP is renaming variables as documented on the Variables From External Sources PHP manual page:

Note: Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].

I'm not sure why this would make a difference, though. The data stored in that variable remains unchanged.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
0

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];
    }
    ?>
Community
  • 1
  • 1
Wrikken
  • 69,272
  • 8
  • 97
  • 136
  • Thanks for your reply... I'm a bit confused... if I pass a string using AJAX like above, it considers this string a variable name? I'm just using post, separating keys and values with ':' and pairs with ';', made a parser on the php side to read the data. – Brandon Frohbieter Jun 17 '10 at 20:19
  • Awesome, thanks. was parsing with code added above (virtually the same). – Brandon Frohbieter Jun 17 '10 at 21:15
  • Hmm, added contentType and it's still spitting back the underscores. Alerting it before I send it shows spaces. I'm totally at a loss. – Brandon Frohbieter Jun 17 '10 at 21:23
  • Erm, nevermind, after rereading your code you added about an hour ago, it seems you don't actually POST anything, the whole string apparantly goes in the URL (if I read the javascript right), which means Codeigniter does some voodoo on it, and I don't know Codeigniter: consider this answer moot for your current problem, and only relevant when you want to preserve POSTed data. I'd us an escape(v) in your javascript though, it should be urlencoded. – Wrikken Jun 17 '10 at 21:46
  • Wrikken, thanks again for your reply. Turns out it was Codeigniter, solution added below. I just assumed it was PHP because of that bit I found in the docs, thought it would be too much of a coincidence to be otherwise. – Brandon Frohbieter Jun 18 '10 at 13:17
-1

Php converts no data but variable names.
Are you sure you cannot change a field name?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345