PHP - Navigation using a switch statement - php

I feel like the following script file should work for navigation on my site, but when I click around on the links nothing loads up and nothing loads by default. How do I fix it?
<html>
<head><title>Your Title</title></head>
<body>
Navigation:
News
Whatever1
<br /><br />
<?php
$id = $_GET;
switch($id)
{
default:
include('home.html');
break;
case "what1":include('whatever1');
break;
case "what2":include('whatever2');
}
?>
</body>
</html>

What are you $_GETing? Also $_GET returns an associative array, and the switch statement takes a variable. You need to specify what it is you get by giving it an id like $_GET['id'].

You need to go in to the $_GET variable and pull out the exact field you want:
$_GET['id']

You don't store the actual GET variable in $id,
$id = $_GET;
should be
$id = $_GET['id'];

Related

Replace Content on Page Based on URL Parameter with PHP

I'd like to replace content within my page based on the URL parameter.
Ideally I'd like to use PHP to get:
if {{parameter is X}} display {{content X}}
if {{parameter is Y}} display {{content Y}}
..for a few pages.
Current set up:
<?php if ($CURRENT_PAGE == "Index") { ?>
<div id="firstDiv">this is the standard page</div>
<?php } ?>
<?php if ($CURRENT_PAGE == "p1") { ?>
<div id-"secondDiv">this is a variation of the page</div>
<?php } ?>
And using include("includes/content.php"); to call the html blocks to the page
The firstDiv displays in index.php as expected, but adding the URL parameter changes nothing - the same div still shows (I'd like it to be replaced with the secondDiv)
It seems $CURRENT_PAGE doesn't like URL parameters - what is the alternative?
Hopefully this makes sense, I'm pretty new to PHP. Happy to provide more details if required.
Thanks in advance for any help.
-- UPDATE --
Thank you for the answers so far!
It seems I missed part of my own code (Thanks to vivek_23 for making me realise this - I'm using a template, excuse me!!)
I have a config file that defines which page is which, as so:
<?php
switch ($_SERVER["SCRIPT_NAME"]) {
case "index.php/?p=1":
$CURRENT_PAGE = "p1";
break;
default:
$CURRENT_PAGE = "Index";
}
?>
Before I learn $_GET, is there a way I can use my current set up?
Thanks again.
-- UPDATE 2 --
I have switched to using the $_GET method, which seems to be working well so far. My issue now is when the parameter is not set it is giving an undefined error. I'll try to remember to update with the fix.
$p = ($_GET['i']);
if($p == "1"){
echo '<div id="firstDiv"><p>this is the first div</p></div>';
}
Thanks to the two answerers below who suggested using $_GET
You can used $_GET like
if($_GET['p']==1){
echo '<div id="firstDiv">this is the standard page</div>';
}else if($_GET['p']==2){
echo '<div id="secondDiv">this is a variation of the page</div>';
}
The other way! you can used basename() with $_SERVER['PHP_SELF']
//echo basename($_SERVER['PHP_SELF']); first execute this and check the result
if(basename($_SERVER['PHP_SELF']) == 'index'){
echo '<div id="firstDiv">this is the standard page</div>';
}else{
echo '<div id="secondDiv">this is a variation of the page</div>';
}
You need to send the parameters on the URL query string, like:
yourdomain.com?p=1
So, with this URL, the query string is "?p=1", where you have a GET parameter named 'p' with a value of '1'.
In PHP to read a GET parameter you can use the associative array $_GET, like this:
$current_page = $_GET['p'];
echo $current_page; // returns '1'
The rest of your logic is OK, you can display one div or the other based on the value of the p parameter.
You can read more about how to read query string parameters here: http://php.net/manual/en/reserved.variables.get.php

Passing unknown link variables to a new page

I have a link, an offer page and a destination page. I need to carry the variables from the original link and input them into the links on the offer page.
original link
www.example.com/offerpage.php?offer=1&aff_id=var1&aff_sub=var2
Where you see var1 and var2, those could be any number.
I'm assuming I could do something like this (this is a total guess, just want to make sure I do it correctly).
<?php
if(array_key_exists('aff_id', $_GET)){
$aff_id = $_GET;
}
else {
$aff_id = '1';
}
?>
Then the links on the offer page would be
www.offer.com/index.php?offer=1&aff_id=<?php echo $aff_id; ?>&aff_sub=<?php echo $aff_sub; ?>
and whats the correct format for doing multiples?
This should probably do what you want:
if (!array_key_exists('aff_id', $_GET)) {
$_GET['aff_id'] = 1;
}
echo http_build_query($_GET);
If the query string is offerpage.php?offer=1&aff_id=var1&aff_sub=var2then the output will be:
offer=1&aff_id=var1&aff_sub=var2
And, if the query string doesn't contain aff_id, i.e. offerpage.php?offer=1&aff_sub=var2 then the output will be:
offer=1&aff_sub=var2&aff_id=1

Multiply pages PHP - Problem

I have a problem when creating a script. I have an index.php file, which controls all other pages. Though, I have a problem using the $_GET[''] with $_POST[''] variable in the pages.
I have a page, called adpanel.php Inside that page, I have use the $_GET function like this:
if($_GET['newad'] == "create"):
//In this, I only want the content showed when the above statement is true.
endif;
The above code, does work. I do know how to show the content, if the $_GET is true. Although, inside the $_GET I have a $_POST function, which will submit through jquery, and return the data back to the adpanel.php page.
I have a problem, that it returns the full page in the status div. Example:
http://awesomescreenshot.com/0f8i95ba0
Below is my index, that controls the pages:
case 'a': // Advertisement Panel
if($_GET['newad']){
if($_POST){
include($settings['pagepath'].'adpanel.php&newad=create');
}
include($settings['pagepath'].'adpanel.php&newad=create');
}
if($_GET['manage']){
getHeader();
include($settings['pagepath'].'manageAds.php');
getFooter();
}else{
getHeader();
include($settings['pagepath'].'adpanel.php');
getFooter();
break;
}
How do I fix this issue?
Try this:
case 'a': // Advertisement Panel
if($_GET['newad']){
if($_POST){
include($settings['pagepath'].'adpanel.php&newad=create');
}
include($settings['pagepath'].'adpanel.php&newad=create');
}
if($_GET['manage']){
getHeader();
include($settings['pagepath'].'manageAds.php');
getFooter();
}else{
getHeader();
include($settings['pagepath'].'adpanel.php');
getFooter();
//break;<--remove break
}
break; //<-- break after if/else so it breaks on case and does not continue
Try using
if(isset($_GET['manage'])){

grab variable from url

i am new to php, but im trying. i need you guys help.
i have the following url in the browser address bar www.dome.com\mypage.php?stu=12234342
i am trying to pass the url from the main page to the select case page call select.php
if i should echo the url i get www.dome.com\select.php. so i have decided to echo $_SERVER['HTTP_REFERER']
instead, this gives me the correct url. how can i echo the variable from www.dome.com\mypage.php?stu=12234342 (12234342)
unto select.php. select.php contains code that needs the $var stu=12234342 in order to display the correct message.
$request_url=$_SERVER['HTTP_REFERER'] ; // takes the url from the browers
echo $request_url;
$cOption = $_GET['id'];
switch($cOption) {
case 1:
echo ' some text';
break;
case 2:
echo ' this page.php';
break;
case 3:
echo 'got it';
break;
default:
echo 'Whoops, didn\'t understand that option: <i>'.$cOption.'</i>';
}
?>
You may use parse_url() and parse_string() to grab the variable from a url:
<?php
//assuming www.dome.com/mypage.php?stu=12234342;
$url=$_SERVER['HTTP_REFERER'];
//parse the url to get the query_string-part
$parsed_url=parse_url($url);
//create variables from the query_string
parse_str($parsed_url['query'], $unsafe_vars);
//use the variables
echo $unsafe_vars['stu'];//outputs 12234342
?>
But note: you can't rely on the availability of HTTP_REFERER.
try
echo $_GET['stu'];
on select.php
That's why you need to call the select.php file like this:
www.dome.com/select.php?stu=12234342
and then you can add:
echo $_GET['stu'];
By the way, you need to research about XSS, because that's a huge vulnerability.

$.ajax trying to get $_GET query variables inside newly loaded ajax page

alright i have a div #pagecontainer which loads another php page page_$var.php into it and $var is the page number, i need to get the url query string variables but i cant seem to retrieve them from the newly page.
example: loc=3&option=5
i just cant grab that inside the new page
1) Make sure your page is actually loading - output some static value regardless of the loc and option
2) print out $_SERVER['QUERY_STRING'] to make sure that your get parameters are getting passed
3) print out a dump of _GET via var_dump($_GET)
Good luck!
In the <head> of your main page, or anywhere else as long your ajax script is able to get it, add this:
<script type="text/javascript">
$_GET = <?php echo json_encode($_GET) ?>;
</script>
You should then be able to use the $_GET variable from Javascript. You can for example test it out in FireBug in FireFox:
console.info($_GET);
console.info($_GET['loc']);
console.info($_GET.loc);
Remember to sanitize the values before using them for something important...
Yes, you can. If page_1.php?loc=3&option=5 is your query string, then $_GET['loc'] and $_GET['option'] are set.
im building it based off of this tutorial
http://tutorialzine.com/2009/09/simple-ajax-website-jquery/
except i have the load_page.php including the page_x.php files
but inside the page_1 file i have it including my connect.php file and then running an sql query "SELECT * FROM pages WHERE Pages.option='mysql_real_escape_string($_GET["option"]);
and i can never get the url query string inside the php file
im loading this as page_1 from the tutorial and am trying to get the GET from the page thats loading this one
here a link
http://imstillreallybored.com/newstuff/pagetest.php?loc=3&option=5
info loads page_1.php which is where im trying to grab the url query string
im connecting to the database and grabbing information only because im setting the $loc and $option to test numbers until i can get the string
<?php
include './connect.php';
$sql = "SELECT
*
FROM
pages
WHERE
pages.option =".mysql_real_escape_string($_GET["option"]);
$result = mysql_query($sql);
if(!$result)
{
echo 'The topic could not be displayed, please try again later.';
}else{
if(mysql_num_rows($result) == 0)
{
echo 'This doesn′t exist.';
}else{
while($row = mysql_fetch_assoc($result))
{
if($_GET["loc"]=="3")
{
//display content for sports section based on option
echo $option.'
<div id="pageContent">
<div class="clear"></div>
<h2>'.$row[header].'</h2>
<p class="meta">
<b>Coach(s)</b>:'.$row[coach].'
</p> <div class="post-img">
<img src="'.$row[image].'" width="640" height="196" alt="image missing" />
</div>
<div class="excerpt">
<p>'.$row[body].'</p>
</div>';
}
}
}
}
?>
Here is a way to get url parameters with javascript.
<script language="javascript">
function gup( name )
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}
From: http://www.netlobo.com/url_query_string_javascript.html

Categories