5

In the following code:

    <script type="text/javascript">
        function updateView(set) {
            $.post("<?php echo base_url("/show_cards/load_page")."/"; ?>"+set, function( data ) {
                $( "#content" ).html( data );
            });
        }
    </script>

'set' is a string variable which can have spaces in it. I'm noticing when it has spaces it's not working correctly. How can I fix this?

EDIT: For clarity, I'd like to keep the spaces intact.

rotaercz
  • 3,403
  • 12
  • 51
  • 84
  • I'd like to pass the string while keeping the spaces. – rotaercz Oct 14 '13 at 04:32
  • It doesn't have to be in the URL, I'm just trying to keep the data intact as it is. – rotaercz Oct 14 '13 at 04:34
  • As you say, I think you should replace the spaces as I have answered and replace all occurrences of the new character with spaces before any other operation with the data, to get the data intact. – Rajesh Paul Oct 14 '13 at 04:38
  • As the URL doesn't support spaces and what you want is much similar to that of URL formatting you should try it. – Rajesh Paul Oct 14 '13 at 04:41

4 Answers4

8

NOTE: that $.trim() is now deprecated for .trim()

Use set.trim() to remove leading or trailing spaces and either

set.replace(/ /g,"+")  

or

encodeURI(set)

to keep the spaces inside the string
(refer When are you supposed to use escape instead of encodeURI / encodeURIComponent?)

To do both in one go just chain them

set.trim().replace(/ /g,"+")

Note you may use %20 instead of the plus if you prefer.

But is it not a parameter? If so, perhaps you want to pass it as

$.post("<?php echo base_url("/show_cards/load_page")."/"; ?>",
  {"set":set.trim().replace(/ /g,"+")},
mplungjan
  • 155,085
  • 27
  • 166
  • 222
  • 2
    This is the only answer that answers this question in both parts. It's not about removing or trimming those spaces only. – Hanky Panky Oct 14 '13 at 04:17
4

You have to replace intermediate space(' ') with '%20' with replace() and eliminate boundary spaces(' ') with trim().

So use the following code-

<script type="text/javascript">
    function updateView(set) {
    set=set.trim().replace(/ /g, '%20');
        $.post("<?php echo base_url("/show_cards/load_page")."/"; ?>"+set, function( data ) {
            $( "#content" ).html( data );
        });
    }
</script>
Rajesh Paul
  • 6,364
  • 6
  • 37
  • 53
2

use Trim

<script type="text/javascript">
        function updateView(set) {
          var set=$.trim(set);// by this  leading or trailing spaces removes  
            $.post("<?php echo base_url("/show_cards/load_page")."/"; ?>"+set, function( data ) {
                $( "#content" ).html( data );
            });
        }
    </script>

you can also use string.replace

  var set=  set.replace(/ /g,"+") ;// like that way in this all the spaces removes
Rituraj ratan
  • 9,947
  • 8
  • 33
  • 52
0

Rajesh Paul did a good job with his answer, i don't know why someone gave you a down vote.

Good answer Rajesh, it worked for me. This is what i did: '&p_na2=' + (Txtva3).trim().replace(/ /g, '%20');

decongh
  • 13
  • 1
  • 8