0

I have this form in my view

<form name="login" action="<?php echo base_url(); ?>households/filter" id="contactForm" method="POST">
                <span class="search-options search-field">
                    <select id="city" name="city" class="filter-field">
                        <option selected="" value="-1">
                        <?php echo $this->lang->line("Filter By City");?>     
                        </option>
                        <option value="Barcelona">Barcelona</option><option value="Madrid">Madrid</option><option value="Valencia">Valencia</option>
                    </select>
                </span>
</form>

and here is the JavaScript for submiting the form

$(document).ready(function() {
        var urlmenu = document.getElementById( 'city' );
            urlmenu.onchange = function() {
                 var myform = document.getElementById('contactForm');
                 myform.submit();
        };
     });

but it not sending anything to the POST My controller is like

function filter() {
    var_dump($this->input->post());
    exit;
}

Can you help me, why am I getting

boolean false
5
  • try $_POST, or $this->input->post('field name'); Commented Jan 3, 2014 at 9:20
  • $this->input->post() : The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist. Commented Jan 3, 2014 at 9:31
  • With $this->input->post() you take all the data inside the post! but the problem is that the post has nothing inside!! Commented Jan 3, 2014 at 9:32
  • Are you sure you have no Javascript errors? Check console. Commented Jan 3, 2014 at 9:33
  • no problem at all in js Commented Jan 3, 2014 at 9:41

4 Answers 4

1

I would comment but its too long for it, try this code I use it for exactly same thing I guess.

note that I use bootstrap

HTML

<form action="localhost/etc" method="post" accept-charset="utf-8" class="form-horizontal">                      
    <div class="control-group">
        <label class="control-label">Language</label>
        <div class="controls">
            <select name="language" class="language" id="admin-category">
                <option value="1" selected="selected">Moonlanguage</option>
                <option value="2">English</option>
                <option value="3">Deutsch</option>
            </select>                               
        </div>
    </div>                      
</form>

JavaScript (jQuery)

$('#admin-category.language').change(
    function(){
         $(this).closest('form').trigger('submit');
});

The most importatnt parts are id and class of a <select>.

Sign up to request clarification or add additional context in comments.

Comments

0

You can't reference that function like that, or iterate through it.

You need to provide an index, e.g. $this->input->post('field_name').

If you want to access all post data, you can use the $_POST variable.

Edit:

In CI 2.1+ you can do the following.

$data = $this->input->post(NULL, TRUE); // Returns all POST items with XSS filter
$data = $this->input->post();           // Returns all POST items without XSS filter 

9 Comments

Yes i know that, and if you can see thats what i am doing! the problem is that is not sending the post data
@cbour Read my answer. You can't use var_dump($this->input->post());
that's not true var_dump($this->input->post()); will dump all post simillar to $_POST
What version of CI are you using?
Var_dump saws everything inside the post
|
0

Try to do this

$(document).ready(function() {
    document.getElementById('city').onchange( function(){ 
        document.getElementById('contactForm').submit();
    }); 
 });

function filter() {
    echo $this->input->post('city');
    exit;
}

Comments

0

use predifined form methods in codeigniter:

<?php $attr = array('name'=>'login', 'id=>'contactForm');?>
<?php echo form_open('households/filter', $attr);?>

instead of your form syntax:

<form name="login" action="<?php echo base_url(); ?>households/filter" id="contactForm" method="POST">

base_url is used to add external entities such as images, scripts, or css to the page.

site_url is used specify a controller in codeigniter.

in form_open() method there is no need to use site_url, the first parameter is automatically treated as controller....

for submission via jquery ajax:

$(document).ready(function() {

   $('#contactForm').submit(function(e) {
      e.preventDefault();
      $.post("<?php echo site_url('households/filter');?>",
      {
          city: $('#city').val()
      },
      function(response)
      {
         //ajax response area
      });
   });

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.