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.
Related
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
<?php
$sPage = $_GET["p"];
//echo ("You picked the page: " . $sPage);
if ($sPage == "") {
$sPage = "home.php";
}
include($sPage);
?>
It came from a php multipage website. I would like to write this same kind of code, but in javascript.
What does this code do?
http://www.tropicalteachers.com/web110/superduper/
this link is where the code came from, the php dynamic one
Okey so let's just start from the top to the bottom. I will try to explain shortly what each php thing does also incase you don't know PHP to well.
$sPage = $_GET["p"];
This code above is getting query parameters that you got in your URL, currently it's getting the query parameters "p" so for example if the url was http://localhost/index.php?p=hola the "$sPage" variable would hold the value "hola".
if($sPage == "") { $sPage = "home.php"; }
Short if statement checking if there was a query parameter with a value, if not we will set the variable value to "home.php"
include($sPage)
So this will litrally just take the file "home.php" in this case and include it in page. So anything that is in the file "home.php" will be displayed on the current page you are at.
To replicate this in javascript it would be similar to using ajax to fetch the content you wanna display. Below i will link to a tutorial that can explain how to accomplish that.
https://www.w3schools.com/jquery/jquery_ajax_load.asp
This doesn't help with the URL part, but that you can google yourself to with the correct termanology
I am having an issue using isset to display content on a page.
My PHP file is called messages.php
I am directing my users with links to this URL: messages.php?inbox using if(isset($_GET['inbox']))
{ } to display the users inbox. Same principle with the other users options such as compose message is: messages.php?compose again using isset
The only problem I have is that I cannot stop people from manually typing stuff like domain.com/messages.php or domain.com/messages.php?somethingrandom.
Is there a way to direct users to messages.php?inbox when they type in the address bar something that isnt assigned to isset?
I did try to use switch but couldnt seem to get it to work properly with how ive laid out my HTML.
An example of the whole file is here http://pastebin.com/SfqN2L7g
I am fairly new to PHP and think I may have gone down the complicated route.
Any advice would be appreciated.
Thanks
The answer you added already would work, but I usually like having an array of valid options which I could maybe check against later on.
$validPages = array('inbox', 'compose');
$pageFound = false;
foreach ($validPages as $validPage) {
if (isset($_GET[$validPage])) {
$pageFound = true;
break;
}
}
if (! $pageFound) {
header('Location: /messages.php?inbox');
}
Thanks to the help of Marcos PĂ©rez Gude, the answer is as follows:
if(isset($_GET['inbox']) || isset($_GET['compose'])){
//Then do below
}else{
header("Location: messages.php?inbox");
exit;
}
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);
I'm trying to include a file to output in a tab on a page. The file itself will pull up just fine, but when I try to add the required querystring to it, it gives me a "failed to open stream: No such file or directory" error.
I've tried just a straight include and tried setting the querystring as a variable. Here's where I'm at right now.
$listingVars = '?mls=' . $_REQUEST['mlid'] . '&lid=0&v=agent';include("agentview.php$listingVars");
Has anyone successfully done this?
You can't include a query string in an include().
Assuming this is a local script, you could use:
$_REQUEST['mls'] = $_REQUEST['mlid'];
$_REQUEST['lid'] = 0;
$_REQUEST['v'] = 'agent';
include("agentview.php");
if it's a remote script on a different server, don't use include.
I created a variable on the 2nd page - and passed it a value on the first page - and it worked for me:
*Page with include: 'index.php'
<?php $type= 'simple'; include('includes/contactform.php'); ?>
*Page included: 'includes/contactform.php'
switch($type){
case 'simple':
//Do something simple
break;
default:
//Do something else
break;
}
I modify the accepted answer given by Frank Farmer a bit to work for different queries:
Include twice would cause problem:
$_REQUEST['mls'] = $_REQUEST['mlid'];
$_REQUEST['lid'] = 0;
$_REQUEST['v'] = 'agent';
include("agentview.php");
//changing the v to another
$_REQUEST['v'] = 'agent2';
include("agentview.php");
For those who encounter this multiple include problem, you could wrap you code inside "agentview.php" in a function:
Inside agentview.php
function abc($mls,$lid,$v){
...your original codes here...
}
file need to call agentview.php
include_once("agentview.php");
abc($_REQUEST['mlid'], 0, 'agent');
abc($_REQUEST['mlid'], 0, 'agent2');
Hope it helps someone encounter the same problem like me and thanks Frank Farmer for the great solution which saved me alot of time.