1

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?
10
  • 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']? Commented Sep 17, 2012 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 Commented Sep 17, 2012 at 11:38
  • @GregorMcKelvie I've never heard that. I would recommend using a GET array instead of explodeing a string. Commented Sep 17, 2012 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 Commented Sep 17, 2012 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). Commented Sep 17, 2012 at 15:02

1 Answer 1

1

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)!

Sign up to request clarification or add additional context in comments.

1 Comment

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.