[英]echo php variable inside jQuery function
i have this PHP variable set in joomla 2.5 template file 我在joomla 2.5模板文件中设置了这个PHP变量
$datestart = date('Y-m-d g:i', strtotime("+1 week"));
$dateend = date('Y-m-d g:i', strtotime("+30 days"));
and when i echo this 当我回应这个
<?php echo json_encode($datestart); ?>
inside a php page it works. 在一个PHP页面内它工作。 I'm trying to include this variable inside a jQuery function of the RS Form component 我试图将此变量包含在RS Form组件的jQuery函数中
$formLayout .= "\n".'<script type="text/javascript" src="'.JURI::root(true).'/components/com_rsform/assets/calbs.js"></script>'."\n";
in the .js file i have 在.js文件中我有
jQuery(function() {
jQuery("#datetimepicker").datetimepicker({
format: "dd MM yyyy - HH:ii P",
showMeridian: true,
autoclose: true,
startDate: <?php echo json_encode($datestart); ?>,
endDate: <?php echo json_encode($dateend); ?>,
minuteStep: 15,
pickerPosition: "bottom-left"
});
});
But it's not working and shows syntax error for the php echo line. 但它没有工作,并显示PHP回声线的语法错误。 How can i solve this ? 我怎么解决这个问题?
You have to assign the php values first to js variables in .php file 您必须首先将php值分配给.php文件中的js变量
<script>
START_Date='<?php echo json_encode($datestart); ?>';
END_Date='<?php echo json_encode($dateend); ?>';
</script>
Now in your js file 现在在你的js文件中
jQuery(function() {
jQuery("#datetimepicker").datetimepicker({
format: "dd MM yyyy - HH:ii P",
showMeridian: true,
autoclose: true,
startDate: START_Date,
endDate: END_Date,
minuteStep: 15,
pickerPosition: "bottom-left"
});
});
OR include your js code in your php file then you can directly use php variables inside your js like this: 或者在你的php文件中包含你的js代码然后你可以在你的js中直接使用php变量,如下所示:
jQuery(function() {
jQuery("#datetimepicker").datetimepicker({
format: "dd MM yyyy - HH:ii P",
showMeridian: true,
autoclose: true,
startDate: '<?php echo json_encode($datestart); ?>',
endDate: '<?php echo json_encode($dateend); ?>',
minuteStep: 15,
pickerPosition: "bottom-left"
});
});
Hi You can not use php code in js file. 嗨您不能在js文件中使用PHP代码。 store value in javascript variable and then use this variable in you jquery function. 将值存储在javascript变量中,然后在jquery函数中使用此变量。
您只需将此值存储到隐藏字段中,然后通过其id值将其输入到.js文件中。
try this code 试试这段代码
<input type="hidden" id="startdate" value="<?php echo json_encode($datestart); ?>"/>
<input type="hidden" id="enddate" value="<?php echo json_encode($dateend); ?>"/>
jQuery(function() {
var start = jQuery("#startdate").val();
var end= jQuery("#enddate").val();
jQuery("#datetimepicker").datetimepicker({
format: "dd MM yyyy - HH:ii P",
showMeridian: true,
autoclose: true,
startDate: start,
endDate: end,
minuteStep: 15,
pickerPosition: "bottom-left"
});
});
you should do it this way:
<?php
$datestart = date('Y-m-d g:i', strtotime("+1 week"));
$dateend = date('Y-m-d g:i', strtotime("+30 days"));
?>
<input id="datestart" type="hidden" name="datestart" value="<?php echo $datestart; ?>">
<input id="dateend" type="hidden" name="dateend" value="<?php echo $dateend; ?>">
<script>
var datestart = document.getElementById('datestart').value;
var dateend = document.getElementById('dateend').value;
jQuery(function() {
jQuery("#datetimepicker").datetimepicker({
format: "dd MM yyyy - HH:ii P",
showMeridian: true,
autoclose: true,
startDate: datestart,
endDate: dateend,
minuteStep: 15,
pickerPosition: "bottom-left"
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.