0

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);
 }
1
  • You can use ajax for it!!! Commented Aug 1, 2015 at 5:05

4 Answers 4

2

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

1 Comment

why it is not passing into controller function.anyone please help
2

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

Comments

1

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

Pass Javascript Variables tp PHP Controller in Code Igniter

Comments

0

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

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.