Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want a URL using Codeigniter that is like this:

example?type=1,2,3,4

I can manually type this in and also navigate to URLs like this and the system works OK. However, I have a form using GET where I want to post multiple values for type. I'm using Javascript to join the inputs.

<form action="example" method="GET" id="type-form">
    <select multiple="multiple" name="_type[]" id="type-array-id-1">
        <option value="1">Type 1</option>
        <option value="2">Type 2</option>
        <option value="3">Type 3</option>
        <option value="4">Type 4</option>
        <option value="5">Type 5</option>
    </select>
    <input type="hidden" name="type" id="type-comma-sep"/>
    <input type="submit" value="Get" />
</form>
<script>
    $("#type-form").bind("submit",function(){
        var type_array = $("#type-array-id-1").val().join(",");
        $("#type-array-id-1").val("");
        $("#type-comma-sep").val(type_array);
    });
</script>

I've used some other SO questions answers to get me this far BUT when I submit the form I get:

example?type=1%2C2%2C3%2C4

My back-end relies on the "," being present in the URL. So how can I force the comma into the URL?

I also tried htaccess rewrite (although not even sure this is right):

RewriteCond %{REQUEST_URI} ^([^\%2C]*)\%2C(.*)$
RewriteRule .* %1,%2 [R=301,L]

And I tried using encodeURIComponent(type_array) and encodeURI(type_array), but it never made a difference.

Also my settings in Codeigniter are as follows:

$config['permitted_uri_chars'] = 'a-z 0-9~%.,:_\-';

And within system/libraries/input.php

if ( ! preg_match('/^[a-z0-9,:_\/-]+$/i', $str))

So...

  1. How can I force the comma in the URL (Javascript is preferable)?
  2. Will the solution work cross-browser?
share|improve this question
Does your back-end absolutely have to rely on the comma being present? Can you not use the $_GET array directly? Or could you decode the value of $_GET['type']? – froddd Sep 17 '12 at 11:30
No it doesn't have to as I can change it, but my understanding is from a common / good practice URL design then type=1,2,3 is better – Gregor McKelvie Sep 17 '12 at 11:38
@GregorMcKelvie I've never heard that. I would recommend using a GET array instead of explodeing a string. – Waleed Khan Sep 17 '12 at 12:07
The better practice would be to use CodeIgniter's URL scheme and avoid using a query string in the URL: codeigniter.com/user_guide/general/urls.html – froddd Sep 17 '12 at 14:55
@froddd Yeh but I've got potentially up to 10 "types" type=1,2,3,4 etc. plus other dynamic variables that affect the results (as I am using the URL to filter results). So CI's URL scheme is not best for this (IMO). – Gregor McKelvie Sep 17 '12 at 15:02
show 5 more comments

1 Answer

up vote 0 down vote accepted

If you do the redirect via Javascript then it should be OK!

$("#type-form").bind("submit",function(){
    var type_array = $("#type-array-id-1").val().join(",");

    window.location = '?type='+type_array;

    return false;
});

If you let the form submit normally, it'll URL encode all the query string parameters, converting your commas.

If you just explicitly set the URL with the commas, it should work OK (or at least it did in my test example above)!

share|improve this answer
Great - thanks! – Gregor McKelvie Sep 17 '12 at 12:19
No problem! Just keep in mind that you'll have to add manually any other input field data you might have in the form as well (if your form changes from this) – manavo Sep 17 '12 at 13:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.