views:

95

answers:

1

I need to preserve tab characters from a textarea through POST Data. It seems that there's no way to differentiate tabs from spaces in the $_POST array, and this is really frustrating me.

I'm using a jQuery plugin from here to allow for tab and shift+tab usage within a textarea. http://teddevito.com/demos/textarea.html

The jQuery plugin is using this as its Tab character:

$.fn.tabby.defaults = {tabString : String.fromCharCode(9)};

For some reason, it shows an individual space instead of each tab character, so all my code formatting is lost:

<textarea name="field0" rows="26" cols="123"><?php
    echo $_POST['field0'];
?></textarea>

This also doesn't work. Apparently the tabs disappear before the data even reaches the str_replace function (the first double quotes is the result from when I press TAB in my text editor):

<textarea name="field0" rows="26" cols="123"><?php
    echo str_replace("    ", "\t", $_POST['field0']);
?></textarea>

The reason I need tabs and not multiple spaces is because my application includes on-line code editor.

Anyone have any ideas? I'm guessing the solution would involve modifying the data with javascript before it's sent through POST, but I haven't the slightest idea how to start.

+3  A: 

Well it's a bit like killing an ant with a bazooka but you can use the base64 encoding before POST the data:

http://plugins.jquery.com/project/base64

and decode it with:

http://www.php.net/manual/en/function.base64-decode.php

It should work really nice but it's increase a lot the size of your request.

Cesar
or you can just substitute "\t" with "\\t" using jQuery before the POST and substitute back with the PHP
Cesar
Thank you very much. I tried the other \t or tab <=> space conversions but they didn't work cross browser. The Base64 is a bit slower, but actually re-displays the correct data even in Internet Explorer.EDIT: Actually, the Base64 thing doesn't re-encode correctly for some reason in Firefox. I'm trying to figure out why... And yes, my document is using UTF-8.
Lotus Notes