How to set a php variable using a jQuery conditional test?

admin

Administrator
Staff member
I am trying to set a php variable using jQuery inside of a php file.

In my php file I want to run a jQuery script that tests if a condition is so and if so it sets a php var to one value or else it sets it to another value so I can use it later on in php.

I'm using this to try and set the size of my background image in wordpress based on window size. For now though, I'm just trying to echo the variable (
Code:
$bg_img_r
). Here is what I have:

Code:
<?php function bg_size() { ?>
    <script>
        jQuery(document).ready(function($) {
            if ($(window).height()>720;  {
                <?php $bg_img_r = wp_get_attachment_image_src( $bg_img_id, 'fullbg' ); ?>
            }
            if ($(window).height()<720; {
                <?php $bg_img_r = wp_get_attachment_image_src( $bg_img_id, 'smallbg' ); ?>
            }
        });
    </script>
<?php } ?>

<?php
    echo $bg_img_r;
?>

When I do this I get an error that
Code:
$bg_img_r
is an undefined variable.