0

I need to pass the version no to javascript from php. But i dont want to use ajax.

$(document).ready(function(){
    init();
})

<body>
...
<input type="hidden" id="version" value="<?php echo $version; ?>
....

<script>
function init() {
   version = $('#version').val();
}
</script>

Can i do in this way? Or, should i use ajax?

3
  • You can do it this way. Commented Dec 2, 2017 at 15:25
  • <input type="hidden" id="version" value=" $version;> Commented Dec 2, 2017 at 15:26
  • Your way works fine. Commented Dec 2, 2017 at 15:33

1 Answer 1

3

If you do so theres no need to put it into a hidden input:

const version = "<?php echo $version; ?>";
Sign up to request clarification or add additional context in comments.

1 Comment

Yea, I usually just have .php output a script block that sets the desired javascript variables. I think that's a little clearer then passing them into a hidden element just to pass them back out to js.

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.