how can I pass my dropdown value to CodeIgniter controller?

I am new to CodeIgniter and want to get value from my View’s new.php dropdown which has entries from an existing database to my controller Newhome.php.

My view’s HTML code goes like this:

<div class="container">
    <form action="<?php echo base_url("index.php/Newhome/download");?>" method="post"  id="createFrm" name="createFrm" onchange="this.form.submit()">
<div class="form-group">
                    <label for="">STATE</label>
                    <select name="state" id="state" class="form-control">
                        <option value="">Select a state</option>
                        <?php 
                        if(!empty($states)) {
                            foreach ($states as $states) {
                                ?>
                                <option value="<?php echo $states['state_id'];?>"><?php echo $states['state_name'];?></option>}
                                <?php
                            }
                        } 
                        ?>
                    </select>
                    <p class="state_error"></p>
                     
                </div>
            

            <div>
                <a href="<?php echo base_url('index.php/Newhome/download')?>" class="btn btn-primary" type="submit">Download</a>
            </div>
</div>

My Newhome controller’s download function goes like this:

public function download(){
    $arrData= $this->input->post('state');
    
    print_r ($arrData);

But I am unable to get any value here in the print_r() function, please suggest some way to do it

Thank you for any contribution in advance.

Answer

Use <button> instead of <a> tag. <a> tag don’t have type property.

<div class="container">
    <form action="<?php echo base_url("index.php/Newhome/download");?>" method="post"  id="createFrm" name="createFrm" onchange="this.form.submit()">
<div class="form-group">
                    <label for="">STATE</label>
                    <select name="state" id="state" class="form-control">
                        <option value="">Select a state</option>
                        <?php 
                        if(!empty($states)) {
                            foreach ($states as $states) {
                                ?>
                                <option value="<?php echo $states['state_id'];?>"><?php echo $states['state_name'];?></option>}
                                <?php
                            }
                        } 
                        ?>
                    </select>
                    <p class="state_error"></p>
                     
                </div>
            

            <div>
                <button class="btn btn-primary" type="submit">Download</button>
            </div>
</div>

Leave a Reply

Your email address will not be published. Required fields are marked *