2

i have this PHP variable set in joomla 2.5 template file

$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. I'm trying to include this variable inside a jQuery function of the RS Form component

$formLayout .= "\n".'<script type="text/javascript" src="'.JURI::root(true).'/components/com_rsform/assets/calbs.js"></script>'."\n";

in the .js file i have

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. How can i solve this ?

4

5 回答 5

7

您必须首先将 php 值分配给 .php 文件中的 js 变量

<script>
START_Date='<?php echo json_encode($datestart); ?>';
END_Date='<?php echo json_encode($dateend); ?>';

</script>

现在在你的 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"

    });
 });

或者在您的 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"

    });
});
于 2013-07-13T10:53:59.947 回答
1

您好您不能在 js 文件中使用 php 代码。将值存储在 javascript 变量中,然后在 jquery 函数中使用此变量。

于 2013-07-13T10:47:56.823 回答
1

您可以简单地将此值存储到隐藏字段中,然后通过其 id 值将其获取到 .js 文件中。

于 2013-07-13T11:06:06.753 回答
0

试试这个代码

<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"
});
});
于 2013-07-13T10:58:04.873 回答
0
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>    
于 2013-07-13T11:01:52.657 回答