0

I have this PHP file wherein I need this javascript variable available (inside the same file) to be pass on my PHP variable, something like this.

myfile.php contais:

    var testing  = ["EWMLMc3ES3I", "RSdKmX2BH7o", "SIXwlxhjaKY", "acp8TbBPVos", "6GpKR4-TLoI", "XLKLkTnKRwg", "6WPELkw5kD0"];

and I want to make it like this

 testing = <?php $new_testing ?>

I need a suggestion maybe a jquery snippet but with something like my scenario which is a javascript in a php file.


edit: additional info, the reason for this is , because there's another javascript codes (not on the same file but rather had it include via external JS) that needs that particular PHP variable. so say here's the logic:

  1. javascript_variable ---> php_variable (passing the javascript variable to php) then,
  2. php_variable --> another_javascript_variable (pass the php to another javascript file)
  3. the another_javascript_variable will the be executed by that external javascript file

3 Answers 3

1

It only goes one way for an "on page load event". In other words, JavaScript (client side code) always renders AFTER PHP (server side code).

A way around this is too use an AJAX POST onload where the client side has finished rendering and returns a response back to the server. (Your array)

4
  • can this be apply on my problem? on the same php file?.. I appreciate you comment Dan, but please can you show me some sample codes for me to understand it and make my reference to research more about this. Commented Jan 31, 2013 at 19:27
  • "A way around this is too use an AJAX POST onload where the client side has finished rendering and returns a response back to the server. (Your array)" - @DanKanze - how is this?.. are you suggesting to run it on a jquery .post ? and I think it will render not on a single PHP file right? :( Commented Jan 31, 2013 at 19:40
  • @MarkBean The context of your question is extremely unclear. You never mentioned why you are doing this or how its being used in the application. Im not sure how you expect anyone to help you further than this. Please improve the quality of your answer so people can help you further. Commented Jan 31, 2013 at 19:43
  • edit: additional info, the reason for this is , because there's another javascript codes (not on the same file but rather had it include via external JS) that needs that particular PHP variable. Commented Jan 31, 2013 at 19:51
0

On your PHP side, use json_encode() to convert your php array to a suitable format to pass it to the javascript.

PHP:

$var = array( 'lorem', 'ipsum', 'dolor');
$json_var = json_encode($var);
$parameter = array( 'js_var' => $json_var );
wp_enqueue_script('my_script');
wp_localize_script('my_script', 'object_name', $parameter); 

Javscript:

<script>
my_var = jQuery.parseJSON(object_name.js_var);
alert(my_var); 
</script>
0

From the way I understand your question, I believe what you're looking for is the php function json_encode

Then, you can basically do what you are trying.

<?php $new_testing = array('one', 'two', 'three', 'four'); ?>

<script>
    var testing = <?php echo json_encode($new_testing); ?>;
</script>

If you're asking "How can my JavaScript pass a variable to my PHP", the answer is AJAX -- you have to make a whole new request.

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.