简体   繁体   中英

print value from view to controller codeigniter with ajax onchange without load page

I want to use condition view with the value from view's CodeIgniter without loading page. I tried tracking error, I give up, I don't know, what's it error

jquery ajax:

var site = '<?php echo site_url();?>';
$(document).ready(function(){
  $("#comboKec").change(function (){
    var selectedText = $(this).find("option:selected").text();
    var selectedValue = $(this).val();

    //alert(selectedValue);
      //$("#res").text(site);

    $.ajax({
        type: 'POST',
        serviceUrl: site+'anggota/index',
        data: {'kec' : selectedValue},
        success: function(data) {
            console.log(selectedValue);
        },
        error: function(msg) {
            console.log('error : '+msg);
        }
    });
  });
});

html:

<select>
<option value="aaaa">1</option>
<option value="bbbb">2</option>
</select>

controller:

$kec = $this->input->post('kec');
if($kec="aaaa"){ echo "Data 1"; }
else if($kec="bbbb"){ echo "Data 2"; }
else { echo "No Data Selected"; }

i think u need to change in two place's to see difference in colsole

  $.ajax({
    type: 'POST',
    serviceUrl: site+'anggota/index',
    data: {'kec' : selectedValue},
    success: function(data) {


     >  console.log(selectedValue);
     >             console.log(selectedValue);

    },
    error: function(msg) {
        console.log('error : '+msg);
    }
});

and give the name to the select as

 > <select name="comboKec">

<option value="aaaa">1</option>
 <option value="bbbb">2</option>
</select>

oh your another mistake is in $kec= "aaaa" you should use $kec=="aaaa" instead !Because with $kec= "aaaa" you are assigning value to copare you shoul use $kec=="aaaa" !

  $kec = $_POST['kec'];


>  if($kec=="aaaa"){ echo "Data 1" ;}
 else if($kec=="bbbb"){ echo "Data 2"; } 
else { echo "No Data Selected" ;}

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