Undefined variable on switch via get method - php

I have a cms admin section with the option comments; when i press comments on the admin menu it should take me to comments.php which includes the code below; by default it should load all my comments on a grid, however when i press comments its not displaying anything the page is blank ? My code:
<?php
if(isset($_GET['source'])){
$source1=$_GET['source'];
if(!empty($source1)){
switch($source1){
case 'add_post':
include"includes/add_posts.php";
break;
case 'edit_post':
include"includes/edit_post.php";
break;
case 'view_all_comments':
include "includes/view_all_comments.php";
break;
default:
include "includes/view_all_comments.php";
}
}
}
?>

when u load comments.php the url doesnt sent any parameter via get, on your code you are checking if the source is not empty which on your case its empty at first. Then preform switch statment, in your case it will not preform the switch as the source is empty and thats why you are not seeing anything. You can fix that by adding an else on your if and include view_all_comments.php or like the code below:
<?php
$source1=isset($_GET['source']);
switch($source1){
case 'add_post':
include"includes/add_posts.php";
break;
case 'edit_post':
include"includes/edit_post.php";
break;
case 'view_all_comments':
include "includes/view_all_comments.php";
break;
default:
include "includes/view_all_comments.php";
}
?>

Related

include php file depending on which link is clicked?

I am trying to include several different php include files in my page attach.php which display 1 include file at a time depending on which link is clicked.
Here's my links:
<div class="hewden_square"></div><h33>Insurance Documents</h33>
<div class="hewden_square"></div><h33>Policy Documents</h33>
next I have my php code which checks which link has been clicked.
<?php
switch(isset($_GET['tab'])) { //switch is just like a bunch of if()s
default: //default case
case '': //default case
include('include/attachments_overview.php'); //include page.html
break; //break, witch means stop
case 'insurance': // page2, if changePage has the value of page2
include('include/upload_insurance/upload_files.php'); //include page2.html
break; //break, witch means stop
case 'policy': // page2, if changePage has the value of page2
include('include/upload_policy/upload_files.php'); //include page2.html
break; //stop it
} //end the switch
?>
my default include file should be shown before any link is clicked and this is:
include('include/attachments_overview.php');
then one or the other should be included depending on which link is clicked.
For some reason my default include file is shown as expected and my other include file
include('include/upload_insurance/upload_files.php');
is shown when I click the link attach.php?tab=insurance
however when I click on my last link attach.php?tab=policy this just seems to show the same include file for my insurance tab
include('include/upload_insurance/upload_files.php');
can someone please show me where I am going wrong or show me a better/more reliable way of doing this? Thanks
<?php
$tb = isset($_GET['tab']) ? $_GET['tab'] : '';
switch($tb) { //switch is just like a bunch of if()s
default: //default case
include('include/attachments_overview.php'); //include page.html
break; //break, witch means stop
case 'insurance': // page2, if changePage has the value of page2
include('include/upload_insurance/upload_files.php'); //include page2.html
break; //break, witch means stop
case 'policy': // page2, if changePage has the value of page2
include('include/upload_policy/upload_files.php'); //include page2.html
break; //stop it
} //end the switch
?>

PHP sessions and includes

I have a really nasty problem with sessions and includes in PHP. I have a page called index.php in which I have a switch function between the different pages:
<?php $p=(isset($_GET["p"])?$p=intval($_GET["p"]):$p=1); switch($p) {
case 1: $pagina = "login_and_registration.php";
break;
case 2: $pagina = "admin/registrazione.php";
break;
case 3: $pagina = "admin/login_test.php";
break;
case 4: $pagina = "logged_gym.php";
break;
case 5: $pagina = "logged_trainer.php";
break;
case 7: $pagina = "logout.php";
break;
case 8: $pagina = "admin/completa_profilo_trainer_sql.php";
break;
case 10: $pagina = "error.php";
break;
case 12: $pagina = "profilo_min.php";
break;
case 13: $pagina = "splash.php";
break;
default: $pagina = "index.php";
break;} ?>
Then down the code in the body of the file i have some includes:
<header class="container_12">
<?php include("login_errors.php")?>
<div id="logo_container" class="grid_4 push_4"></div></header >
And this one:
<section>
<?php include_once($pagina);?>
</section>
When I fill the form which is in this page, corresponding to page 1 in the switch function, i get this error on the login (where I start a new session with a name):
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Applications/MAMP/htdocs/FitnessOptimize/index.php:63) in /Applications/MAMP/htdocs/FitnessOptimize/admin/login_test.php on line 73
In the index.php on line 63 i have the first include that I just posted you. Any ideas on how to avoid this bug? Also my header ("Location: "); don't work i think for the same reason. Any help will be appreciated! Thanks to you all!
No output before sending headers!
the header error is caused if you have typed anything before header ,even white spave before the <?php will cause the error
for more information check the Headers already sent by PHP question on the stackoverfow
check this link to Having two different sessions in same domain
use session_start(); function at the top of the page on which you are using $_SESSION
.Be ensure that you donot have a single space before session_start()
in php first of all all the header transfers and than the text is sent to browser if there is a single space is present your session will not be started and you cannot use $_SESSION[] so write this at the top of the page
session_start();
should be at the beginning of the page
The problem may be caused by unprintable characters at the start of code, or you're writing some value in header.

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'])){

Dreamweaver, PHP, MySQL

I have downloaded a backup copy of my cpanel webserver. I install WAMP on my local laptop.The database and php username and password have been set. Then I loaded the database into the local MySQL database. I verified the phpinfo.php is working. I setup dreamweaver manage site with the extracted website on my laptop. Now the back end admin pages on my laptop for my website are working. However the frontend is not completely loading and I can't figure out why.
Please go here to see what the page looks like since I can't post images on this post. dev.quicksalenetwork.com/images/frontend.png
Now if you go to the live development site you would see what is should look like.
Also here is the source code of the index.php page.
<?php
//redirect for install
if(!file_exists('Connections/myconn.php'))
header('Location:install.php');
//install file check
/*if(file_exists('install.php') || file_exists('install2.php') || file_exists('install3.php'))
print '<font color="red"><strong>Please delete install files { install.php , install1.php, install2.php }</strong></font><br>';
*/
//*******************************************************************************
//PHP Real Estate Classifieds
//All Rights reserved by Quick Sale Network inc.
//Do not modify, duplicate, resell with out direct permission from Quick Sale Network inc.
//Script is bound by terms listed in terms.html file
//*******************************************************************************
//fetch template header
include('header.php');
//define varable
define('go','');
//set index varable for switch statment
if (isset($_REQUEST['go']))
$go = $_REQUEST['go']; // requested page
else
$go = ''; // default
switch($go) {
case 'gmap':
include('gmap.php');
break;
case 'photos':
include('photos.php');
break;
case 'resource_center':
include('resource_center.php');
break;
case 'linkdir':
include('linkdir.php');
break;
case 'mailme':
include('formmail.php');
break;
case 'sell':
include('sell.php');
break;
case 'buy':
include('buy.php');
break;
case 'properties':
include('properties.php');
break;
case 'login' :
header("location: members/login.php");
break;
case 'register' :
include('register.php');
break;
case 'pricing' :
include('pricing.php');
break;
case 'calc' :
include('calc.php');
break;
case 'contact' :
include('contact.php');
break;
case 'faq' :
include('faq.php');
break;
case 'features' :
include('features.php');
break;
case 'member_login' :
include('member_login.php');
break;
case 'mailme' :
echo"<br><h4>";
include('formmail.php');
echo"</h4>";
break;
case 'detail' :
include('detail.php');
break;
case 'search' :
include('search.php');
break;
/*case 'home_gmap' :
include('home.php');*/
default:
include('home_gmap.php');
break;
}//end of switch statment
//fetch template footer
include('footer_city.php');
?>
Any ideas what I am doing wrong?
My guess:
your code uses php short open tags (<? instead of <?php) and you haven't enabled short tags in your wamp install..
There's a short_open_tag directive in your php.ini to enable it
I had the same thing happen to me. It happens when a file is corrupted. Just delete the file (Blanking it wont work) and copy all the code back into it.

How to call switch function from using PHP

I have a ajax.php file that contains the following sample code:
switch($_REQUEST['request_name'])
{
case 'edit':
echo "edit mode";
break;
case 'delete':
echo "delete mode";
break;
default:
die("Error: wrong request name ".$_REQUEST['request_name']);
break;
}
I have another file index.php that I want to call results from ajax.php. Hmm.. how do I do it? I normally use javascript to call results from ajax.php. But is there a way I can call results from within index.php as well? Code is wrong below but something to this effect.
$result = include("ajax.php?request_name=delete");
echo $result;
You were correct to use include but instead of passing in the vars like a query string you can just define them before your include and they'll be brought in to the file.
$_REQUEST['request_name'] = 'edit';
include('ajax.php');
Any variables that are then defined in your included file will now be available in the parent file as well. If you were to process the edit action and store the results in a var called $results in ajax.php you would have access to that same variable from within the including file (after the include statement).
$_REQUEST['request_name'] = 'edit';
include('ajax.php');
echo $results;

Categories