pass query string get value into url link - php

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>";
}
?>

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>";
}
?>

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

Related

Passing variables to another page with url - PHP

I want to execute some PHP code by clicking on a link and Passing variables to another page with url
Pleas help me.
Such WordPress Site: https://www.armandl.com/?p=5123
You set $_SESSION['status'] to 0 and then set it to 1, so it will always be 1. Additionally, the link you click has nothing to do with the session vars.
Page 1:
Normally to get just one variable from one page to another via hyperlink you would add it to the URL as a query parameter and access it using $_GET:
<?php
echo 'No';
echo 'Yes';
?>
Page 2:
You also need to check if $_GET['status'] is set:
<?php
if (isset($_GET['status']) && $_GET['status'] == 0) {
echo "No !";
}
elseif (isset($_GET['status']) && $_GET['status'] == 1) {
echo "Yes !";
}
else {
echo "NOT set !";
}
?>
Now, if you want it to be accessible on other pages without passing in the URL, then set it as a session var:
session_start();
$_SESSION['status'] = $_GET['status'];
You can also use any type of variable & string instead of the 0,1 selection.
Example:
if (isset($GET["newpwd"])){
if ($_GET["newpwd"] == "passwordupdated"){
//code goes here
}
}
To pass from another page you would use something like:
header("Location: ../signup.php?newpwd=passwordupdated");

How to include 2 checks in a if statement

Basically I am coding a script where it simply redirects the user to the destination page. And I want to be able to check if multiple websites are not equal to the value; if this is so, it will run a error, else it will proceed.
I can't seem to get this to work though, although I am sure there's a way to check multiple values.
<?php
$url = $_GET['site']; // gets the site URL the user is being redirected to.
if ($url != "***.co", "***.net")
{
echo ("Website is not valid for redirection.");
} else {
echo ("You are being redirected to: " . $url);
}
?>
You can make an array of items to check for and then check if the url is in the array:
if (!in_array($url, array("***.co", "***.net")))
{
}
You can also use multiple conditions like #wrigby showed, but the solution using an array makes it easier to add more (or a dynamic number of) urls. But if there are always two, his is better.
You'll need two complete conditionals, connected with a logical and (&&) operator:
<?php
$url = $_GET['site']; // gets the site URL the user is being redirected to.
if ($url != "***.co" && $url != "***.net")
{
echo ("Website is not valid for redirection.");
} else {
echo ("You are being redirected to: " . $url);
}
?>

Show message with PHP when a parameter is passed in the URL

As new in PHP, I have a simple question. Am I able to do something like this?
E.g. the user, is redirected to a link like this:
http://website.com/directory?parameter
When that parameter is in the URL, I want a message to appear somewhere in the website, when the parameter is missing, just hide it.
get parameters are stored in the $_GET variable. So you can check if a get parameter is set with:
if(isset($_GET['parameter'])) {
echo 'parameter is set';
}
else {
echo 'parameter is not set';
}
When you pass a parameter like this:
http://site.com/page.php?this=that
You create what is called a GET request. In order to echo what's in the request, you'd do:
<?php echo $_GET['this']; ?>
In this instance, this would output:
that
To read more about PHP superglobals like $_GET, check out this link.
<?php
// Suppose this is URL http://site.com/page.php?url_var=val;
if(isset($_GET["url_var"]))
{
$msg = "your message";
}
else
{
$msg = "";
}
// NOW JUST PRINT $MSG WHERE EVER YOU WANT WITH OUT ANY CONDITION;
echo $msg;
?>

passing values through the URL when using the $_GET just get blank page

this is the code used to get the information from the database, the images I have taken off the echo statement for the time being, and just the name of the product. When I click on the product name it sends me to cart.php and should pass the value in the URL it shows in the browser when I hover over the text but when i click it send me to cart.php and just shows a blank page
$product_types = get_all_subjects2(); function is just the query
while($products = mysql_fetch_array($product_types))
{
$name = $products['name'];
$address = $products['image_location'];
echo '<ul>';
echo "<li><a href=\"http://localhost/project/cart.php?subj=" . urlencode($products["name"]) .
"\">{$products["name"]}</a></li>";
The code
<?php
if (isset($_POST['subj']))
{
$a = $_POST['subj'];
echo $a;
}
else {
echo"error";
}
?>
$_POST stored values following POST request. If you navigate via a plain link, you're doing GET requests. So look in $_GET.
Also it might be a good idea to always output valid HTML. Otherwise the browser might or might not render what you're outputing.
You send your arguments via GET. So asking for $_POST isn't the right idea ;).
For Debbugging it's nice to have this statement in your target site:
echo '<pre>', print_r($_REQUEST), '</pre>';
Best,
Christian
Addition: tested the code.. just works fine:
<?php
echo '<pre>', print_r($_REQUEST), '</pre>';
if (isset($_GET['subj'])) {
$a = $_GET['subj'];
echo $a;
} else {
echo 'error';
}
?>

To manipulate URL based on coming URL by PHP

How can you manipulate the destination URL by the name of the starting url?
My URL is
www.example.com/index.php?ask_question
I do the following manipulation in the destination URL
if ($_GET['ask_question']) {
// Problem HERE, since if -clause is always false
if ( $login_cookie_original == $login_cookie )
{
include "/codes/handlers/handle_login_status.php";
header("Location: /codes/index.php?ask_question");
die("logged in - send your question now");
}
}
if (isset($_GET['ask_question'])) {
...
If you did a print_r() of $_GET you would see
Array
(
[ask_question] =>
)
which shows that ask_question is set, but is empty, so it tests false.
$location = "test.php";
if(isset($_SERVER['QUERY_STRING']))
{
header("Location:".$location . "?" . $_SERVER['QUERY_STRING']);
}
else
{
header("Location:".$location);
}
I think you want to replace that with:
if (isset($_GET['ask_question'])) {
Which will only be true if it's contained in the URL.
you could possibly check the $_SERVER['QUERY_STRING'] variable to see if it contains 'ask_question'
edit: fixed the typo
You can retrieve values after the question mark by using the $_GET super global. For the example ?ask_question=true.
//is ask_question true?
if($_GET['ask_question'] == 'true') {
echo 'ask_question is true';
} else {
echo 'ask_question is not true';
}
For variables without values (like ?hello), use $_GET in such way:
if(isset($_GET['hello'])) {
echo 'hello is there';
} else {
echo 'hello is not there';
}
You've asked a lot of very basic questions about PHP and you don't seem to have a grasp on how the language works. I suggest giving the documentation a good read before your next question.
PHP Documention Home
PHP: Variables From External Sources

Categories