1

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.

7
  • 1
    you need to submit the form to get the value Commented Mar 10, 2021 at 13:08
  • Yes i have a button doing it! But not reflected here Commented Mar 10, 2021 at 15:18
  • Share your complete view code Commented Mar 10, 2021 at 16:43
  • @mail2bapi I have edited and added full view code Commented Mar 11, 2021 at 4:43
  • <a> tag cannot submit form use <button> tag Commented Mar 11, 2021 at 5:03

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome. If this helps you please mark it as right answer.
0

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()">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.