/    Sign up×
Community /Pin to ProfileBookmark

passing variables between two php files

Hi,

I son’t know what I am doing wrong here. I have two php files, one has the data extracted from MySQL, I want to pass the variables to a second php file.

Here are the codes:

In first file


$dataVal1 = max($values);
$dataVal2 = ($values);

print $dataVal1.'<br />’;
print $dataVal2.'<br />’;

// Here both dataVal1 and dataVal2 prints out correctly
// Now I pass these two values to the other file like this
echo ‘<a href=”http://www.yadayada.yada/xyz.php?val1=$dataVal1&amp;val2=$dataVal2>link</a>’;

In the second php file, I wrote:
$value1=$_GET[‘val1’];
$value2=$_
GET[‘val2’];

But this file never gets the any data in value1 and value2.

What am I doing wrong?

Thanks in advance

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscOct 02.2008 — In your above example, the values passed to the second php script are:

$_GET['val1'] = "$dataVal1" (not a variable, but a string - the actual value being passed has a dollar sign [$] in front of it).

You can't put php variables into the URL like that. If you actually want to send these variables to the second php file using $_GET, then generate the link from your first php file:

[code=php]
<?php
$link = "http://www.yadayada.yada/xyz.php?val1=".urlencode($dataVal1)."&val2=".urlencode($dataVal2).";

echo "<a href=".$link.">link</a>";
?>
[/code]
Copy linkTweet thisAlerts:
@chestertbOct 02.2008 — First, you're missing the closing quote.

Next, the problem may be that you're including raw php variables in your html. Your val1 will be received by xyz.php as '$dataVal1'.

I use this...
[code=html]<a href="http://www.yadayada.yada/xyz.php?val1=<?=$dataVal1?>&amp;val2=<?=$dataVal2?>" >link</a>[/code]
...which works on my server. However, I understand that this is deprecated, and it might not work on your server.

If that's the case, construct your html as a php variable as follows;

[code=php]$output = "<a href='http://www.yadayada.yada/xyz.php?val1=".$dataVal1."&amp;val2=".$dataVal2."' >link</a>";
print $output;[/code]
Copy linkTweet thisAlerts:
@MacPCauthorOct 02.2008 — Hi thank all for the replies.

Unfortunately, I couldn't get neither method to work, then I tried this:

In the first file:

echo '<a href= "ttp://www.yadayada.yada/xyz.php'.'? val1 ='.$dataVal1.'& val2 ='.$dataVal2.'/>click</a>';

In the second file:

$value1=$_GET['val1']; //not $val1, not a typo

$value2=$_
GET['val2']; //Same

Now it works. I don't even know how and why but it does. wicked. ?
Copy linkTweet thisAlerts:
@ariellOct 02.2008 — ... you did exactly what aj was suggesting (except for his " typo). He first encoded and used the RESULTING var, while you FORMED the var directly.
Copy linkTweet thisAlerts:
@MacPCauthorOct 02.2008 — I see. It get a bit confusing. So there is nothing wrong with this code then. Thank you all again.
×

Success!

Help @MacPC spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...