Reputation: 1364
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.
Upvotes: 0
Views: 1113
Reputation: 1364
@Repox I did figure out an approach last night which was actually pretty similar to your solution. Here's what I did:
$("#find-btn").on("click", function() {
var fab = $("select#fabrikat").val();
var type = $("select#type").val();
var model = $("select#model").val();
var produktnummer = $("select#produktnummer").val();
// we have to replace spaces in models and types
model = model.replace(/\s+/g, '-');
type = type.replace(/\s+/g, '-');
// produktnummer can be empty
produktnummer = (produktnummer === '') ? "0" : produktnummer;
// we also have to URI encode the model and prodnr since it
// can be horror strings like 'xy762 / ø23'
model = encodeURIComponent(model);
produktnummer = encodeURIComponent(produktnummer);
// our url string to update the href
var url = fab + '/' + type + '/' + model + '/' + produktnummer;
// update the href with the specific parameters
$(this).attr("href", "http://app.dev/kategorier/index/"+url);
});
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:
'EX+ 340-34 / Ø14' OR '77773+ -/ 23IK'
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.
Upvotes: 0
Reputation: 15476
You can't really achieve this without using some JavaScript:
<script type="text/javascript">
function update_selections()
{
var base_url = '<?php echo site_url('shop/kategorier/index'); ?>/' + $('#fabrikat').val() + '/' + $('#type').val() + '/' + $('#model').val();
if( $('#produktnummer').val() != "" )
{
base_url = base_url + '/' + $('#produktnummer').val();
}
document.location.href = base_url;
}
</script>
Remove the current form and use an onclick event to trigger this.
Upvotes: 1