Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7816359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:50:23+00:00 2026-06-02T05:50:23+00:00

I would like my PHP script to be able to access the width of

  • 0

I would like my PHP script to be able to access the width of the browser window. I’ve been reading up on this, and PHP can’t access this information itself, but Javascript/jQuery can, and can then pass it to the server using AJAX, so PHP can get at it.

Following a few solutions online I’ve written the following test file, and called it “test.php”

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>  
</head>

<?php
if (!isset($_POST["window_width"])) {
?>

<script type="text/javascript">
    var window_width = $(window).width();
    $.ajax({
        type: "POST",
        data: "window_width="+window_width,
    });
</script>   

<?php
}

if(!isset($_POST["window_width"])) {
    echo "not set";
}

?>
</html>

Loading test.php displays “not set” which shows that the window_width variable is not being picked up by PHP. This seems weird to me, because Firebug shows that the variable is there (set at 1366 on my computer, as this is the width of my browser).

How can I ensure that $_POST[“window_width”] is set so that I can access it using PHP?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:50:24+00:00Added an answer on June 2, 2026 at 5:50 am

    I don’t think you need to use ajax in this case, the ‘outer’ (?) page needs to receive the data, not the ‘inner’ ajax request.

    <?php session_start(); ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>  
    </head>
    
    <?php
    if (!isset($_POST["window_width"]) && !isset($_SESSION['window_width'])) {
    ?>
    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" id="window_width_form" style="display:none;">
    <input name="window_width" id="window_width" value="" />
    </form>
    <script type="text/javascript">
        $(function(){
            $('#window_width').val($(window).width());
            $('#window_width_form').submit();
        });
    </script>   
    </body>
    <?php
    }
    elseif (isset($_POST['window_width']) && !isset($_SESSION['window_width']))
    {
        $_SESSION['window_width'] = $_POST['window_width'];
        exit(header("Location: {$_SERVER['PHP_SELF']}\r\n"));
    }
    
    if(!isset($_POST["window_width"]) && !isset($_SESSION['window_width'])) {
        echo "not set";
    }
    
    ?>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to be able to start a second script (either PHP or
I would like to make a php script that can capture a page from
I would like to access a MySQL database using PHP. Can someone please explain
i would like to learn how to make PHP/CSS syntax generator. i assume this
I would like to allow users register and login so they can access more
I would like to host a few 3rd party PHP scripts on a Windows
I have php scripts that do things like register, login, upload. I would like
I would like to provide a PHP hanging request that will stay open until
I would like to know if PHP is Object Oriented, I read that from
I would like to make a website that is php and only requires one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.