简体   繁体   中英

Codeigniter JavaScript issue when submitting a form

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

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> .

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

You need to provide an index, eg $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 

Try to do this

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

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

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
      });
   });

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