Pass value from a select to a link’s GET parameter

Asked

Viewed 854 times

0

Hello, I needed to pass the value of one to a parameter of a link, this to be sent via GET. I am using the codeigniter framework.

Follow the code I have:

<div class="col-xs-12 col-md-3 form-num-sala">
    <div class="row">
        <div class="col-xs-12 col-md-8" style="margin-left:-55px;">
            <div class="form-group">
                <label>Designação da sala</label>
                <br/>
                <select name="designacaoSala" class="form-control">
                    <?php foreach ($designacaoSala as $numsala) { ?>
                        <option value="<?php echo $numsala->numerosala ?>">
                            <?php echo $numsala->numerosala ?>
                        </option>
                        <?php } ?>
                </select>
            </div>
        </div>
        <div class="col-xs-12 col-md-4" style="margin-left:-25px;">
            <label>Aplicar</label>
            <br/>
            <a href="<?php echo site_url('utilizador/addSalaEspacoEvento/?idplano='.$hDefenidos->idplanoespacoevento.'&numerosala='); ?>">
                <button class="form-control"><i class="fa fa-check"></i></button>
            </a>
        </div>
    </div>

</div>

The variable $hDefenidos->idplanoespacoevento comes from above but the variable &numerosala should come from select with the name "designacaosala" and be the diced to the following link:

<a href="<?php echo site_url('utilizador/addSalaEspacoEvento/?idplano='.$hDefenidos->idplanoespacoevento.'&numerosala='); ?>">
    <button class="form-control"><i class="fa fa-check"></i></button>
</a>

Can someone back me up? Thanks

2 answers

0

If there is no problem in going through POST and not GET, you can do so: Instead of passing the variables in a link, pass the values in the form. When you use the url in codeigniter you can take the values by the segments (each "/" separates a segment, where the first one comes right after the site’s base url). Example: www.site.com/segment1/segment2/segment3/... where segment1 is the name of the controller, segment2 the name of the controller method and the segment3 a parameter for the method. The select value you take via POST.

VIEW: (I will use only the functional part disregarding the formatting div’s)

<form method="POST" action="<?=site_url('utilizador/addSalaEspacoEvento/'.$hDefenidos->idplanoespacoevento); ?>">
    <select name="designacaoSala" class="form-control">
        <?php foreach ($designacaoSala as $numsala) { ?>
            <option value="<?php echo $numsala->numerosala ?>">
                <?php echo $numsala->numerosala ?>
            </option>
            <?php } ?>
    </select>
    <input type="submit" value="Aplicar" />
</form>

CODEIGNITER CONTROLLER:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Utilizador extends CI_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function addSalaEspacoEvento($idplanoespacoevento) {
        $valorDoSelect = $this->input->post('designacaoSala');

        ...
    }
}

If you are using the codeigniter routes, I suggest you review them if you have problems finding the method in the request. Remember that lib Session must be loaded, either on the controller or in the autoload file.

I hope I’ve helped. :)

0

Hello, I understand you want to pass the option value of the selected room to the link URL, no? I made a solution in jquery. The logic would be to take the link displayed by PHP, take the selected room and assign it to the link when another option is selected. Note: I omitted PHP codes. I added a class for select and an id for the link tag, to select them with jquery. Link to the code: (https://jsfiddle.net/tr139sv8/)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.