www.webdeveloper.com
Results 1 to 6 of 6

Thread: require() based on query string values

  1. #1
    Join Date
    May 2003
    Location
    Florida
    Posts
    19

    require() based on query string values

    I would like to set a page that prints a header based on the client's screen resolution. I am attempting to do this with some JavaScript on the index.htm page that retrieves the resolution information and passes it, via a query string, to a php page that uses selection to require() one of two headers depending on the query string variable's value.

    *****Script to get resolution*****

    Code:
    <script language="JavaScript" type="text/javascript">
    
    function choose_header() {
    	if (window.screen.width==800 && window.screen.height==600) {
    		window.location.href="/mc/dev/hometest.php?sr=1"
    	} else {
    		window.location.href="/mc/dev/hometest.php?sr=2"
    	}
    }
    
    </script>
    *****************************

    ******Script to print header******
    Code:
    <?php
    	function print_header($res) {
    		if ($res == 1) {
    			require('header_800.inc');
    		} elseif (res == 2) {
    			require('header_1024.inc');
    		} else {
    			echo 	'</head>'."/n".
    					'<body>'."/n".
    					"<h1>res: $res</h1>";
    		}
    	
    	$reso = $_GET['sr'];
    	
    	if (isset($reso)) {
    		print_header($reso);
    	} else {
    		echo '</head><body><h1>$reso is not set.</h1>';
    	}
    ?>
    *****************************

    The problem is that the server does not render the php script at all. I set up a simple php page with a single require() statement which printed one of the aforementioned headers:
    Code:
    <?php
    	require('header_1024.inc');
    ?>
    This worked just fine.

    I also wrote a script to try and test values of the query string variable:
    Code:
    <?php
    	
    	$reso = $_GET['sr'];
    	
    	if (empty($reso)) {
    		echo '<h1>$reso is empty.</h1>';
    	} elseif (isset($reso)) {
    		echo '<h1>$reso is'.$reso.'</h1>'
    	}
    ?>
    This script was not rendered either.

    Examples can be seen using the following links:
    hometest.php (redirect via index.htm: query string value passed)
    hometest.php (direct link: no query string value passed)
    myphp.php (test page for include file and php engine)
    foo.php (script to test query string values: no values passed)

    Questions........

    Have I passed the value from the JavaScript correctly?

    Have I retrieved the variable correctly in the php scripts?

    Why aren't hometest.php and foo.php rendered while myphp.php is?

    Any help is greatly appreciated.

  2. #2
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    You're javascript looks ok... For your PHP, try this:

    PHP Code:
    if (isset($_GET["sr"])) {
        
    $sr $_GET["sr"];
        if (
    $sr == 1) {
            require(
    'header_800.inc');
        }
        else if (
    $sr == 2) {
            require(
    'header_1024.inc');
        }
        else {
            echo 
    "header not set";
        }


  3. #3
    Join Date
    May 2003
    Location
    Florida
    Posts
    19

    Issue Resolved

    Thanks, pyro: the new php did the trick. I still don't understand why the old code was failing; I'd like to know so that I don't make the same mistake again. Was it possibly that I used single quotes rather than double on the $_GET index?

    Thanks again for the fix.

  4. #4
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    This part is going to be a problem:

    } elseif (res == 2) {

    should be:

    } elseif ($res == 2) {

  5. #5
    Join Date
    May 2003
    Location
    Florida
    Posts
    19

    hence the screen name...

    I can't believe I missed that. I must have checked over that tiny script 100 times.


    Thanks again.

  6. #6
    Join Date
    Dec 2002
    Location
    High on life
    Posts
    10,104
    You're welcome...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles