Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ?

share|improve this question
1  
in js file you can not enter php code. –  Ravi Jul 13 '13 at 10:45
 
what error is it showing ? –  Vivek Jul 13 '13 at 10:46
 
If your JavaScript file is not processed by PHP, then the <?php ... ?> won't be replaced. Hence the syntax error. –  Felix Kling Jul 13 '13 at 10:49
add comment

5 Answers

up vote 0 down vote accepted

You have to assign the php values first to js variables in .php file

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

</scritp>

Now in your js file

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

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"

});
});
share|improve this answer
 
Thanks assigning it first to js variables worked. –  Kannan Naidu Jul 13 '13 at 11:36
add comment

Hi You can not use php code in js file. store value in javascript variable and then use this variable in you jquery function.

share|improve this answer
add comment

You can simply store this value into the hidden field and then get it by its id value in to the .js file.

share|improve this answer
add comment

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"
});
});
share|improve this answer
add comment
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>    
share|improve this answer
 
this method worked too but i didn't want to include the hidden input type –  Kannan Naidu Jul 13 '13 at 11:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.