[英]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. 我将如何在控制器函数中使用此javascript变量var textBoxVal = $(this).val()。
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. 借助ajax,您可以做到。
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. 请使用ajax。 This problem has already been discussed here.
这个问题已经在这里讨论过了。 Please refer it.
请参考。
Pass Javascript Variables tp PHP Controller in Code Igniter 在代码点火器中传递Javascript变量tp PHP控制器
What you're doing wrong here is $(this).val()
to pass on the input's value. 您在这里做错的是
$(this).val()
传递输入的值。
The this
object's scope keeps on changing, and is quite different when you try to access in 在
this
对象的范围不断变化的,而且是相当不同的,当您尝试访问
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. 解决方案非常简单(因为您已经将
textBoxVal
初始化为输入的值。
$('#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);
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.