简体   繁体   中英

how to pass selected option id value to codeigniter controller

i want to pass selected option value in id to codeigniter controller.

<p>
        <select id="quantity" name="quantity" tabindex="2" onchange="calculate(this)" required autofocus>
            <option value="">Choose Your Quantity</option>
            <?php
                if($prodqty)
                {
                    foreach($prodqty as $qty)
                    {
                        for($i = $qty->quantity_from; $i <= $qty->quantity_to; $i++)
                        {   
            ?>
            <option value="<?=$i?>" id="<?=$qty->discount?>"><?=$i?></option>
            <?php } } } ?>
        </select>
      </p>

i am already getter selected option value, now i want to get id value also ie id="discount?>"

function add_cart_prod()
{
    if(isset($_POST['submit']))
    {

this is controller where i want to get id value

If you subitted the data via POST, which I suppose, because you test the POST-Array, you should get them this way:

$this->input->get_post('quantity');

But perhaps you used in your HTML the option GET for the form submission, then this should work:

$this->input->get('quantity');

If you want to get both values XSS-clean you should add a second paramenter, which is set to TRUE:

$this->input->get_post('quantity',TRUE);

As discussed below you should change the value of the option to:

<option value="<?=$i?>_<?=$qty->discount?>"><?=$i?></option>

And then explode the array by this char: "_" to get the two values:

$valuearray = explode ( "_" , $this->input->get_post('quantity'));

$valuearray[0] should contain your $i-part and $valuearray[1] the discount.

Important is, that the delimiter-char cannot be a value of either $i or $qty->discount. Otherwise choose a different char

You should try this, maybe it will work, Inside the calculate(this) function:

var discount = $("select[name='quantity'] option:selected").attr('id');
alert( discount );
$("#discount").val( discount ); //this will save the discount value in the hidden field

EDIT :

Put a hidden field in your form to contain the discount value

<input type="hidden" name="discount" id="discount">

Now, submit the form as usual. Hope it helps.

Use ajax call on change event of the selection of the options:

Just Changed your code little :

<select id="quantity" name="quantity" tabindex="2" onchange="calculate(this)" required autofocus>
            <option value="0">Choose Your Quantity</option>
            <?php
                if( !empty($prodqty)):
                 foreach($prodqty as $qty):
                        for($i = $qty->quantity_from; $i <= $qty->quantity_to; $i++): ?>
                         <option value="<?php echo $i?>" id="<?php echo $qty->discount?>"><?php echo $i?></option>
                        <?php endfor;
                    endforeach;
                  endif; ?>
        </select>

Your javascript function :

<script type="text/javascript">

function calculate(id)
{
    var id=id;
    $.ajax({
            type:'POST',
            url:'your controller/add_cart_prod',
            data:{'id':id},
            success:function(data){
                // the next thing you want to do 
            }
        });
}
</script>

Your Controller Function:

function add_cart_prod()
{
 $id=$this->input->post('id',true);

 //send this value to your model 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM