-1

I can't find the best way to pass a JavaScript variable to use in a PHP file.

I found this function wp_localize_script(), but it does exactly the contrary.

Is there a way to pass a JavaScript variable to PHP?

  • This isn't a WordPress specific question and is probably better off on stack overflow proper. That said, the answer is no, not without making another request to the server. See this answer on stack overflow. – Andrew Bartel Jan 12 '16 at 22:04
  • In wordpress, to respect best practices – Pablo Levin Jan 12 '16 at 22:04
  • 1
    What exactly are you trying to get from javascript? – jgraup Jan 12 '16 at 22:30
  • the post id that I have hosted in an attribute – Pablo Levin Jan 13 '16 at 15:52
  • The post id should already be available from the php end. See get_the_ID(). If it's not, could you post some example code that we could take a look at? – Andrew Bartel Jan 13 '16 at 22:07
0

You could use cookies - set in JS, and read from PHP. Another way can be using an AJAX request to get the variable value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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