0

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>"; 
 }
?>
asked Sep 12, 2014 at 15:35
2
  • 1
    where does merchantid (is $ missing?!) come from? Commented 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?? Commented Sep 12, 2014 at 15:41

3 Answers 3

1

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>";
 }
?>
answered Sep 12, 2014 at 15:38
5
  • where is $merchantid defined? Commented 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. Commented 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 edit Commented 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 feedback Commented 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. Commented Sep 12, 2014 at 17:16
1

$_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.

answered Sep 12, 2014 at 15:46
0

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.

answered Sep 12, 2014 at 15:42
2
  • you should check isset($_GET['merchantid']) otherwise either of your solutions could produce warnings Commented 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. Commented Sep 12, 2014 at 15:53

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.