简体   繁体   中英

how will i pass a javascript variable to codeigniter controller function

i cant pass the variable to my controller function.please help

<input type="button" id="mydate" name="mydate" value="<?php echo $dat;?>" class="monthYearPicker" />



script:
$('#mydate').on('click', function () {

  var textBoxVal = $(this).val();
  $.ajax({
    type: 'POST',
    url: '<?php echo base_url();?>Money_c/selectallbudget', // link to CI function
    data: {
        val: $(this).val()
    },
    success: function (msg) {

        console.log(msg);
    }
  });

});

how will i take this javascript variable var textBoxVal = $(this).val() in my controller function.

controller:

public function selectallbudget(){
   $mydate= $this->input->post('val');

   $sendMe = $this->money_m->selectbudget($mydate);

   echo json_encode($sendMe);
 }

With the help of ajax you can do it.


 FIRST: -------------------------------- <!--write this code in you header--> <input type="hidden" value="<?php echo base_url(); ?>" id="baseurl"/> 
<!--your javascript code-->
<script type="text/javascript">
var base_url = $('#baseurl').val();
var dateValue = $('#mydate').val();

$.ajax({
    url: base_url + "controller_name/function_name",  // define here controller then function name
    method: 'POST',
    data: { date: dateValue },    // pass here your date variable into controller
    success:function(result) {
        alert(result); // alert your date variable value here
    }
});
</script>
<!--your controller function-->
public function budget()
{
    $dat = $_POST['date'];
    echo $dat;
}


--------------------------------
SECOND: if you want to load any html code then you use this method otherwise above first method
--------------------------------
<!--write this code in you header-->
<input type="hidden" value="<?php echo base_url(); ?>" id="baseurl"/>


<!--your javascript code-->
<script type="text/javascript">
var base_url = $('#baseurl').val();
var dateValue = $('#mydate').val();

$( "#mydate" ).load( 
    base_url + "controller_name/function_name",  // define here controller then function name
    { date: dateValue },    // pass here your date variable into controller
    function(){ 
        // write code on success of load function
    }
);
</script>
<!--your controller function-->
public function budget()
{
    $dat = $_POST['date'];
    echo $dat;
}

Your code is almost okay. There is only one change in your controller code:

Controller:

public function selectallbudget(){
   //$mydate= $_POST['val']; This is not PHP this is codeigniter

   $mydate =  $this->input->post('val');

   $sendMe = $this->money_m->selectbudget($mydate);

   echo json_encode($sendMe);
 }

Please make use of ajax. This problem has already been discussed here. Please refer it.

Pass Javascript Variables tp PHP Controller in Code Igniter

What you're doing wrong here is $(this).val() to pass on the input's value.

The this object's scope keeps on changing, and is quite different when you try to access in

data: {
    val: $(this).val()
}

It's rather trying to access the current object at hand.

Solution's pretty simple though (since you're already initialising textBoxVal to the input's value.

$('#mydate').on('click', function () {
    var textBoxVal = $(this).val();
    $.ajax({
        type: 'POST',
        url: '<?php echo base_url();?>Money_c/selectallbudget', // link to CI function
        data: {
            val: textBoxVal
        },
        success: function (msg) {
            console.log(msg);
        }
    });
});

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