简体   繁体   中英

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.

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>

Move this onchange="this.form.submit()" from the form tag to the select :

<select name="state" id="state" class="form-control" onchange="this.form.submit()">

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