1

I'm trying to check screen width and assign it to a php variable to do some if else statements. This is what I got.

<script>
    var mobileFormWidthCheck = $(window).width();
    var mobileFormReady;
    if(mobileFormWidthCheck < 767){
        mobileFormReady = 22;
    }
    else{
        mobileFormReady = 55;  
    }

</script>

    <?php   
        $widthChecked = "<script>document.write(mobileFormReady);</script>";
        echo $widthChecked;
    ?>

This works perfectly. But when I try to echo something based on the mobileFormReady value, it doesnt echo.

This is what im trying to get to work.

$widthChecked = "<script>document.write(mobileFormReady);</script>";
if($widthChecked == "22"){
  echo 'this page is under 767 pixels';
}
else if($widthChecked == "55"){
  echo 'this page is OVER 767 pixels';
}
else{
  echo 'NOT WORKING YET';
}

I think its a string integer issue. But I cant seem to figure it out. Can you guys please help me? Thanks a lot.

3
  • It isn't string-integer issue. Commented Apr 15, 2013 at 3:57
  • you'll need to use ajax for this Commented Apr 15, 2013 at 4:00
  • I think you might be missing some basic concepts here... Look at duplicate suggested by PleaseStand, it explains it pretty well. Commented Apr 15, 2013 at 4:00

2 Answers 2

3

I guess you can use cookies for it.

1) First add a cookie jquery plugin.

2) Then store that window width in a cookie variable.

3) Access your cookie in PHP like $_COOKIE['variable name'].(http://www.w3schools.com/php/php_cookies.asp)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Someone answered.
0

You cannot achieve what you want...

The reason being

$widthChecked  is "<script>document.write(mobileFormReady);</script>";
and not 22 or 55.

The reason you are getting the value on echo is because it is executing the script and not printing the value of $widhcheck

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.