I am trying to pass in a $_GET variable from a query string and pass it into a link to another page that has an application on it.
A customer will be directed to my page and the url will have the variable name merchantid. I need to take that on the home page, and pass it to the application page.
I've got it displaying on the home page as a test, so I know how to get it. I just need to know how to pass it the application page.
<?php
if (empty($_GET)) {
// no data passed by get
echo "<a href='{site_url}application'>Application</a>";
}
else
{
// The value of the variable name is found
echo "<a href='{site_url}application?merchantid=" .merchantid ."'><Application></a>";
}
?>
My else link actually blows up currently.
Ok, here is my second try, with the same result. The link blows up when I pass in the merchantid into the url. Ex. www.mysite.com/?=merchantid=12345
<?php
if (empty($_GET)) {
// no data passed by get
echo "<a href='{site_url}application'>Application</a>";
}
else
{
if(isset($_GET['merchantid'])){$merchantid = $_GET['merchantid'];}
else{$merchantid = "DefaultMerchant";}
echo "<a href='{$site_url}application?merchantid=" .$merchantid ."'><Application </a>";
}
?>
-
1where does merchantid (is $ missing?!) come from?fast– fast2014年09月12日 15:37:29 +00:00Commented Sep 12, 2014 at 15:37
-
The problem is not clear - do you call the PHP with merchantid as a query parameter and ask how to access it? Does the PHP produce error messages??fast– fast2014年09月12日 15:41:50 +00:00Commented Sep 12, 2014 at 15:41
3 Answers 3
Why your code is not working
You're not telling php that "merchantid" is a variable nor you're defining it.
Solution
Replace
echo "<a href='{site_url}application?merchantid=" .merchantid ."'><Application></a>";
With
if(isset($_GET['merchantid'])){$merchantid = $_GET['merchantid'];}
else{$merchantid = "";}
echo "<a href='{$site_url}application?merchantid=" .$merchantid ."'><Application></a>";
}
Updated code
<?php
$site_url = 'http://'.$_SERVER['HTTP_HOST'].'/';
if (empty($_GET)) {
// no data passed by get
echo "<a href='{$site_url}application'>Application</a>";
}
else
{
if(isset($_GET['merchantid'])){$merchantid = $_GET['merchantid'];}
else{$merchantid = "DefaultMerchant";}
echo "<a href='{$site_url}application?merchantid=".$merchantid."'>Application</a>";
}
?>
-
where is
$merchantid
defined?andrew– andrew2014年09月12日 15:40:25 +00:00Commented Sep 12, 2014 at 15:40 -
unless you've previously assigned $_GET['merchatid'] to the variable $merchantid, you should use $_GET['merchantid'] instead. Also, dont forget the isset() check to avoid php notices.MSTannu– MSTannu2014年09月12日 15:43:10 +00:00Commented Sep 12, 2014 at 15:43
-
There is still a problem with this code; if
$_GET['merchantid']
is not defined then$merchantid
will not get defined and so will produce warnings when you try to read from it. +1 for editandrew– andrew2014年09月12日 15:54:42 +00:00Commented Sep 12, 2014 at 15:54 -
@andrew in the previous version of the answer the link wasn't echoed if $_GET['merchantid'] wasn't set, anyway I updated again my answer, this will output in any case the link :) Thanks for your feedbackuser3456807– user34568072014年09月12日 15:59:29 +00:00Commented Sep 12, 2014 at 15:59
-
See my revised code. I'm confused about how to display the link in my inner If statement. It looks like I'm not setting anything for the If statement.C.Coggins– C.Coggins2014年09月12日 17:16:30 +00:00Commented Sep 12, 2014 at 17:16
$_GET is an array indexed by whatever values are in the query string. For example:
http://sit.url.com?merchantId=12&foo=bar
would place the following in the $_GET array:
$_GET['merchantId'] = "12"
$_GET['foo'] = "bar"
You will want a block in your code to initialize a $merchantId variable based on the presence of those values from $_GET:
//folks commonly use ternaries for this:
$merchantId = (isset($_GET['merchantId'])) ? $_GET['merchantId'] : false
Which is a shorthand way of stating:
if (isset($_GET['merhantId']) {
$merchantId = $_GET['merchantId']
} else {
$merchantId = false;
}
As Angelo and C.Coggins mentioned, don't forget the "$" in front of your variable in php.
You either need to assign $_GET['merchantid']
to $merchantid
first, or replace $merchantid
with $_GET['merchantid']
unless you have register_globals turned on, which you really shouldn't use.
So either add this:
$merchantid = $_GET['merchantid'];
or use this:
echo "<a href='{$site_url}application?merchantid=" . $_GET['merchantid'] . "'><Application></a>";
Besides that, as others pointed out, your original code is missing a $
before the variable name.
-
you should check
isset($_GET['merchantid'])
otherwise either of your solutions could produce warningsandrew– andrew2014年09月12日 15:45:14 +00:00Commented Sep 12, 2014 at 15:45 -
True. I was only meaning to point out how to get the
$_GET['merchantid']
value as the OP requested.Schlaus– Schlaus2014年09月12日 15:53:40 +00:00Commented Sep 12, 2014 at 15:53