Jump to content

[SOLVED] URL Get: Pass a variable name to another variable in a URL?


foochuck

Recommended Posts

I would like to pass $var1 to $masterVar so that both variables are exact copies - however I want to do this though a URL...(using the get method)...

 

$var1 = "My text goes here";

 

http://www.thewebsite.com?masterVar=var1

 

However when I "get" $masterVar it comes out reading a string "var1" - I want it to read "My text goes here"

 

Please help!

 

FOO

Link to comment
Share on other sites

show your actual code (in code tags).  It looks to me like you're using single quotes around it so it's interpreting $var1 literally. You need to either have it in double quotes or not in quotes at all:

 


echo "http://www.thewebsite.com/project.php?masterVar=$var1";

// or

echo "http://www.thewebsite.com/project.php?masterVar=" . $var1;

Link to comment
Share on other sites

I tried this suggestion but it didn't work. I didn't receive any errors - and with this method not even $var1 showed up.

 

use this

 

$var1 = "My text here";
$masterVar = $$_GET['masterVar'];
echo $masterVar;

 

And the url will be

http://www.thewebsite.com?masterVar=var1

 

Ray

Link to comment
Share on other sites

show your actual code from source and target scripts (use code tags) seriously I think we're making a way bigger issue out of this than it really is.  I mean, the only way it would literally echo out "$var" instead of the value of $var is if you're a) trying to echo out the link (with the variable) in single quotes, or b) your link is just plain text outside of php tags (regular html).  There's really no other reason, except for that your script isn't being parsed at all, which it obviously is, as something is being received and echoed out.

Link to comment
Share on other sites

Page 1:

 

$var1 = "This is my text";

 

http://www.thewebsite.com/projects.php?masterVar=var1

 

Project.php Page:

 

$masterVar = $_GET["masterVar"];

echo "$masterVar"; // This reads $var1 - I'd like it to read This is my text

Link to comment
Share on other sites

Page 1:

 

$var1 = "This is my text";

 

http://www.thewebsite.com/projects.php?masterVar=var1

 

Project.php Page:

 

$masterVar = $_GET["masterVar"];

echo "$masterVar"; // This reads $var1 - I'd like it to read This is my text

 

Okay man, I don't mean to sound rude, but you got to focus here.... You keep showing separate little chunks of code.  That does nothing for us. You need to copy/paste your entire code.  You said earlier you are echoing your link, with quotes, without quotes, etc.. but you just keep pasting a link out of context.

 

Not counting security issues, this is what your code should look like, no more, no less:

 

page1.php

<?php
$var1 = "This is my text";

echo "<a href = 'http://www.thewebsite.com/projects.php?masterVar=$var1'>my link</a>";
?>

 

projects.php

<?php
$masterVar = $_GET['masterVar'];

echo $masterVar; // output: This is my text
?>

 

Link to comment
Share on other sites

umm...do you mean to say that you forgot the

 

</a>

 

?

 

There's no possible way that that would cause your variable to be passed literally.  The only three ways it could be passed literally is

 

a) you had single quotes around it

b) it was not wrapped in <?php ... ?> tags as an echo. 

c) your server doesn't recognize and therefor isn't parsing it (which we ruled out)

 

Those are the ONLY three ways.  I'm just trying to make this very clear for you, because I don't want you walking away thinking that php code breaks like that when not closing an html tag.  It could make what's echoed out look funny markup-wise, but it cannot in any way determine literal vs. figurative interpretation.

 

Link to comment
Share on other sites

Understood. Thanks Crayon.

 

umm...do you mean to say that you forgot the

 

</a>

 

?

 

There's no possible way that that would cause your variable to be passed literally.  The only three ways it could be passed literally is

 

a) you had single quotes around it

b) it was not wrapped in <?php ... ?> tags as an echo. 

c) your server doesn't recognize and therefor isn't parsing it (which we ruled out)

 

Those are the ONLY three ways.  I'm just trying to make this very clear for you, because I don't want you walking away thinking that php code breaks like that when not closing an html tag.  It could make what's echoed out look funny markup-wise, but it cannot in any way determine literal vs. figurative interpretation.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.