I have a form with 4 dropdowns and a submit button. When a user clicks the submit button I want to go to my controller handling the input/output giving the users choices, but with a correct URI.
The dropdowns are dynamically generated from Ajax depending on the users choices, bar the first dropdown. They are working absolutely fine.
The method handling the user input is located in this controller: /app/controllers/shop/kategorier, index method looks like this:
public function index($fabrikat, $type, $model, $produktnr = '0')
{
// get all the data from the model
$data['all'] = $this->fetch->get_kategorier($fabrikat, $type, $model, $produktnr);
print_r($data['all']);
}
My goal is this: When users hit the submit button, I wan’t them to view the page with that exact URL including the parameters, eg. http://app.com/shop/kategorier/index/LG/WASH/FL9834X/0/ That’s all I really want.
Note: Accessing this URL manually works fine, and the controller/model handles the parameters as they should.
How do I accomplish this? I tried two things, they both failed. Sort of.
-
I tried with jQuery, and generated a encodedURL string with all the
parameters with a ‘/’ delimiter, and changed the .attr(‘href’) on
submit. But that was a nightmare, since some of the $model’s can be
eg.'CE1000C-T / XEE'etc. -
I tried to make a ‘proxy’ method in the Kategorier
controller taking all the input->posts and appending them to a
redirect('kategorier/'.$fabrikat.'/'.$etc), but that was rather
stupid.
I would rather not use query strings like /kategorier?fabrikat=LG&type=WASH etc
My form looks like this:
<?php echo form_open('shop/kategorier/'); ?>
<select id="fabrikat" name="fabrikat">
<option name="0">Vælg Fabrikat</option>
<?php foreach ($fabrikater as $f) : ?>
<option value="<?php echo $f->Fabrikat; ?>" name="<?php echo $f->Fabrikat; ?>"><?php echo $f->Fabrikat; ?></option>
<?php endforeach; ?>
</select>
<select id="type" name="type">
</select>
<select id="model" name="model">
</select>
<div id="produktnummer-wrap" style="display:none">
<select id="produktnummer" name="produktnummer">
<option name="0" value="">Vælg Produktnummer</option>
</select>
</div>
<?php echo form_submit('findparts', 'Find Reservedele'); ?>
<?php echo form_close(); ?>
I believe I’m somehow overcomplicating things, but right now I can’t think straight. I will be more than happy to elaborate anything.
I really hope someone is able to help me out, thanks in advance.
@Repox I did figure out an approach last night which was actually pretty similar to your solution. Here’s what I did:
Although I need to be very wary when handling the parameters in my controllers and models, they need to be encoded and decoded correctly afterwards. A slight portion of the model strings could be something like:
Which isn’t ideal with the decoding in my controller using
str_replace('-', ' ', $str);I therefore need to examine lots of the data before choosing a replacement character.However, this is probably the best solution if one wishes to do this without having to use query strings in Codeigniter.