0

How can I declare a global variable in a PHP file and access directly in jQuery?

HTML

<script type="text/javascript">   
   var date_selected = <?php echo $from; ?>;
</script>

JS

$(function() {
   alert("dsdsadasdas" + date_selected);
   var date = date_selected;
   var startDate;
   var endDate;
});

Because on my test its not working.

2
  • any error in the browser console Commented Jul 11, 2013 at 3:11
  • have you included the file that holds your global variable into your accessed file? Commented Jul 11, 2013 at 3:15

1 Answer 1

4

You are missing the quotes

var date_selected = '<?php echo $a; ?>';
Sign up to request clarification or add additional context in comments.

1 Comment

or echo json_encode($a); this would properly escape any ' in $a

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.