Unable to grab URL parameters using _GET - php

First time posting and I am fairly new at using PHP so please bare with me. If this needs further explanation, just let me know.
I have a page (let's call it page1.php) that has the following global variable declared:
$online = true;
When a link on page1.php is clicked, I want to pass a parameter (called method) to the following page (let's call it page2.php). If page2.php is accessed via a link from page1.php specifically, I want it to load with this parameter in place and trigger a certain behavior.
Update: added on 3/16/21
The link on page1.php is included in a template file that is used on multiple pages. So I have used a php if statement to append the parameter to the link if the page has a global variable of $online = true:
<a href="page2.php<?php if(isset(GLOBALS['online'])) {?>?method=online<?php } ?>"?link</a>
So far, I have successfully (I think) passed the parameter to page2.php (page2.php?method=online). At the top of page2.php, I have the following code in place to assign the value of this parameter to a variable called $method:
$method = $_GET['method'];
I expected this to make $method = "online" but unfortunately, when I do this and try to echo $method, I get the following notice:
Notice: Undefined index: method in
C:URL\index.php on
line 7
This seemed fairly straight forward when I started but this is driving me crazy. What am I missing? Thanks in advance!

this way works for me:
page1.php
<!DOCTYPE html>
<html>
<body>
link
</body>
</html>
page2.php
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
$method = false; // empty
if(array_key_exists('method', $_GET) and !empty($_GET['method']))
$method = $_GET['method'];
var_dump($method);
// https://yoururl/page2.php?method=online
// string(6) "online"
// https://yoururl/page2.php?method=
// bool(false)
// https://yoururl/page2.php
// bool(false)

Update your code like this
// if it does not exist.
$method = $_GET['method'] ?? null;
PHP Null coalescing operator
Keep trying and play with the language, you are in the right direction

Related

2 Include code into 1 line php sql

I have a problem with the following code. How do i put 2 included files into 1 line include?
I already tried it like the code below but nothing is shown (blank page). It should show the url.
Url: theme/default/main/index.php (it would be)
application-sql-realtime.php = (should show 'default' or anything else when user changing their themes template it connect to sql)
<?php include('./theme/<?php include_once("config/application-sql-realtime.php");?>/main/index.php');?>
Refer image for code
include_one returns boolean
Maybe can be like this:
<?php
$theme = exec("php config/application-sql-realtime.php");
include("./theme/{$theme}/main/index.php");
?>
But I think you better put it in some class/function
<?php
// inside application-sql-realtime.php you declare a function to return theme name eg: getThemeName
include("config/application-sql-realtime.php");
$theme = getThemeName();
include("./theme/{$theme}/main/index.php");
?>
Make a class with a static or non static method to retrive the value you need (e.g. 'default'). Then you can implement this value in your string to include the first file. Following an example of a static method call:
<?php
$val = EgClass::getPathPart();
include('./theme/'.$val.'/main/index.php');
?>

PHP isset not picking up url passed parameter

I'm using the following code in the file main.php:
searchpage = "index.php?k=SEARCH";
include($searchpage);
To call code within index:
if(isset($k)){
$k = $_GET['k'];
if ($k =="SEARCH"){
include("searchpage.php");
}
}
By my understanding this should insert the contents of the file searchpage.php where I called
include($searchpage);
However it just loads main.php again, meaning that k was not set. How does passing ?k=var work in regards to isset() and why does isset reading k as null when I put k=SEARCH in the url when calling the page?
If you need more of the code for context or clarity for the question please let me know thank you.
Try this, I think $k is not set so PHP doesn't go into the "if clause":
if(isset($_GET['k'])){
$k = $_GET['k'];
if ($k == "SEARCH"){
include("searchpage.php");
}
}
main.php
$k="search";
include "index.php";
index.php
if ($k=="search") {
include "searchpage.php";
}
Sorry, I might be missing your point, but from the logic of your code, this is simpler and works fine.
You cannot use this $searchpage = "index.php?k=SEARCH";
If you want to pass parameter you have to pass it from url i.e from address bar only.
So pass parameter in address bar and then include your index.php like this
$searchpage = "index.php";
Then your code wil work fine
Hope this will help you.

PHP How to convert a controller url call to a function

I am trying to integrate two pieces of code together. The existing code already generates a hash code and the function is called with this URL
header(Location: http://".$_SERVER['HTTP_HOST']."/subfolder/controller.php?document&validate&patient_id=".$pid."&document_id=".$nid."&process=true");
Is there another way to execute this function without doing a header redirect because the header redirect is causing the code to halt processing upon redirect.
In the controller file there is only one line echo Controller::act($_GET);
I tried to convert it to a function. I tried.
include_once controller.php //class file
function hash_tag($pid, $nid){
$filetag = "document&validate&patient_id=".$pid."&document_id=".$nid."&process=true";
echo Controller::act($filetag);
}
hashtag($pid, $nid);
Any help would be greatly appreciated.
Code for the Controller.class.php file can be seen here
https://github.com/juggernautsei/Drag-N-Drop/blob/master/library/classes/Controller.class.php
You didn't post your code inside controller.php but considering that your first approach was accessing that code via url parameters, I'll assume that you are executing variables in that code as GET variables (example: $_GET['patient_id']).
If that is the case, now that you are executing that code via include_once you have to change the way you set your variables in controller.php because there is not more $_GET.
You are trying to pass the string from the GET request to the controller.
But Contract::act() works on an array ($_GET is a superglobal array).
Build an array inside the function and assign the parameters of the function to it, then pass it to the controller, like so:
include_once 'controller.php';
function hash_tag($pid, $nid)
{
$array = array();
$array['document'] = 1;
$array['validate'] = 1;
$array['patient_id'] = $pid:
$array['document_id'] = $nid;
$array['process'] = 1;
echo Controller::act($array);
}
hashtag($pid, $nid);

Passing php variable from one file to another?

I have a variable set in my main file (main.php), and need the second file (uploads.php) to reference the variable as it is set in the first file. It is returning undefined right now tho.
The second file is loaded with $.load into the first file: code example below -
Main.php Contents:
<?php $accountName = get_option('account_name'); ?>
<div id="uploads"></div>
Load Your Playlist
function loadUploadsFile() {
jQuery('#uploads').load('uploads.php');
}
Uploads.php file contents
<?php echo $account_name; ?> <== returns undefined
$url = 'http://www.somewebsite.com/' . $accountName . '/page/'
/* more code below running a query/loop etc. */
As you may be able to tell, I want Uploads.php to reference the variable decleration in Main.php but is is not pulling the value, it is just returning undefined. Uploads.php loads into the uploads div, but without the account name set the content is just blank.
Would I need to pass the variable to Uploads.php through ajax? I've tried session variables but couldn't get that to work. I was trying an ajax request but I am new to it so couldn't get that nailed. Any help would be great.
Session variables should work after all. Make sure you have the following:
In Main.php
session_start();
$_SESSION["account_name"] = "test";
In Uploads.php
echo $_SESSION["account_name"];
You could pass it as a GET parameter in your jQuery call like:
jQuery('#uploads').load('uploads.php?account=<?php echo($accountName);?>');
And then in your uploads.php file get it from the request like:
$accountName = $_GET['account'];
Some other options are storing it in the session, setting a cookie, or using jQuery .post to send it as POST data.
Since you are doing jQuery, you might want to look at http://api.jquery.com/jQuery.post/.
Your code should be something like (not sure if exactly)
$.post('uploads.php', { account_name: "<?= get_option('account_name') ?>" }, function(data) {
$('.result').html(data);
});
And then you can use this variable in your uploads.php by referencing $_POST['account_name'].
PS: I assume you are having PHP 5.4x
Save the account name in a session
Main.php
<?php
session_start();
$accountName = get_option('account_name');
$_SESSION['accountName'] = $accountName;
?>
Uploads.php
<?php echo $_SESSION['accountName']; ?>

trying to pass header into php file not working

I begin with vars.php:
<?php
include('goo.php');
$googl = new goo_gl('http://myurl.info?=sdfdsfs'.uniqid());
$url1 = $googl->result();
$link=$url1;
$message=$msgarray[rand(0,count($msgarray)-1)];
$picture="http://img6.imageshack.us/img6/4540/7.jpg";
?>
I want to feel http://mmyurl.info?=sdfdsfs'.uniqid() into the goo.gl api to spit out a shortened url
and then use this information in vars.php which is in the header''
this info is then used on another page where $link is called, but i can never get it to work properly
You should give a query string variable a name first:
$googl = new goo_gl('http://myurl.info?myvar=sdfdsfs'.uniqid());
Now you can access value of myvar via $_GET['myvar'].

Categories