0

I'm trying to read the a variable/value that was passed in the URL.

as an experiment, I started with 2 simple files.

test.php

<?php
//option1
//include "http://myhost.com/test2.php?tempvar=testonly";

//option2
//$tempvar = "testonly";
//include "http://myhost.com/test2.php";

//option3
$tempvar = "testonly";
include ("test2.php)";
?>

test2.php

<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    console.log(getURLParameter("tempvar"));
    $("#test").html(getURLParameter("tempvar"));

    function getURLParameter(name) {
        return (decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search.toLowerCase())||[,""])[1].replace(/\+/g, '%20'))).toUpperCase()||"";
    }
});
</script>
<div id="test"><?php echo tempvar; ?></div>

some observations:

  1. If I call http://myhost.com/test.html?tempvar=testonly from my browser tab, it works fine (i.e. testonly is displayed on the page)

  2. if I call http://myhost.com/test.php from my browser, no "testonly" is displayed on my page. So it seems my getURLParameter javascript function is not finding the tempvar that was passed via include. According to the include manual (example 3), this should work.

  3. Only option3 works in my test.php BUT in my situation, this is useless to me as test2.php can be in another host. and it's possible test2.php can be replaced by another programming language like java (.jsp) or coldfusion or even just plain HTML and DB communication is handled through AJAX calls.

According to phpinfo, the server is using PHP 5.1.6. maybe I need a specific PHP extension to make it work?

Thanks a lot

1 Answer 1

1

change

<?php echo tempvar; ?>

To

<?php echo $tempvar; ?>

Btw, you are just declaring a variable and include the second file. If you want to pass a variable from one page to another you need to use:

Depends how you pass the variable (GET or POST)

GET

<?php echo $_GET['tempvar'];?>

POST

<?php echo $_POST['tempvar'];?>

Or you can use for both

<?php echo $_REQUEST['tempvar'];?>
Sign up to request clarification or add additional context in comments.

Comments

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.