I am receving an undefined error on a $_POST method [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
This is strange but I am posting an input using the $_POST method which is below:
$c = count($_POST['gridValues']);
But the problem is that I am receiving an error stating:
Notice: Undefined index: gridValues in /web/stud/..../ on line 40
(which is line above)
How come I am receiving this error because the $_POST method is definitly correct?
Below is the whole code:
<?php
ini_set('session.gc_maxlifetime',12*60*60);
ini_set('session.gc_divisor', '1');
ini_set('session.gc_probability', '1');
ini_set('session.cookie_lifetime', '0');
require_once 'init.php';
ini_set('display_errors',1);
error_reporting(E_ALL);
session_start();
?>
$i = 0;
$c = count($_POST['gridValues']);
for($i = 0; $i < $c; $i++ ){
switch ($_POST['gridValues'][$i]){
case "3":
$selected_option = "A-C";
break;
case "4":
$selected_option = "A-D";
break;
case "5":
$selected_option = "A-E";
break;
}
}

You need to check if it is set first:
<?php
if(isset($_POST['gridValues'])) {
$c = count($_POST['gridValues']);
}
?>

There are a few ways to prevent a notice level message about an undefined variable.
Put # in front of the variable. ex: $c = #$_POST['gridValues'] This can be dangerous as you are explicitly telling it to ignore any problems with the variable. See the PHP manual for more info on #: http://php.net/manual/en/language.operators.errorcontrol.php
Turn off (or change) error reporting. ex: ini_set('display_errors', 0);
Check if the variable is defined before using it. ex:
$c = '';
if(!empty($_POST['gridValues'])){
$c = count($_POST['gridValues']);
}

Related

PHP session error A session had already been started and Undefined variable: _session

I want to create from with captcha. If captcha not correct then show same valaue in form.
This is page for add data to session. It can show data from session such as echo $_session['txtShippingFirstName']; here.
// Use captcha
if(!((md5((isset($_POST['captcha'])?$_POST['captcha']:"")) == $_SESSION['captchaKey']) || (isset($_SESSION['plaincart_customer_id'])))){
$_session['txtShippingFirstName'] = $_POST['txtShippingFirstName'];
$_session['txtShippingLastName'] = $_POST['txtShippingLastName'];
$_session['txtShippingEmail'] = $_POST['txtShippingEmail'];
$_session['txtShippingAddress1'] = $_POST['txtShippingAddress1'];
$_session['txtShippingAddress2'] = $_POST['txtShippingAddress2'];
$_session['txtShippingPhone'] = $_POST['txtShippingPhone'];
$_session['txtShippingCity'] = $_POST['txtShippingCity'];
$_session['txtShippingState'] = $_POST['txtShippingState'];
$_session['txtShippingPostalCode'] = $_POST['txtShippingPostalCode'];
//setError($_session['txtShippingFirstName']);
setError("CAPTCHA not correct");
header("Location: checkout.php?step=1");
}else{
$includeFile = 'checkoutConfirmation.php';
}
This is form page if captcha not correct then return to this page
if(isset($_session['txtShippingFirstName'])){
$customerFirstName = $_session['txtShippingFirstName'];
$customerLastName = $_session['txtShippingLastName'];
$customerEmail = $_session['txtShippingEmail'];
$customerAddress = $_session['txtShippingAddress1'];
//$_session['txtShippingAddress2']
$customerPhone = $_session['txtShippingPhone'];
$customerCity = $_session['txtShippingCity'];
$customerState = $_session['txtShippingState'];
$customerPostalCode = $_session['txtShippingPostalCode'];
}
echo $_session['txtShippingFirstName'];
It show error Undefined variable: _session on line echo $_session['txtShippingFirstName']; . If I add session_start(); on the top of page it show error Notice: A session had already been started - ignoring session_start() because I add session_start in header file already. How to fix this error ?
The PHP superglobals need to be in uppercase:
$_SESSION['txtShippingFirstName']
in this case.

Learning PHP template sites [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
So I know there are plenty of engines all ready out there like smarty that will do this for me but I want to build a basic one just for my own learning rather than just jumping on someone else's code for now.
So far I have a set of pages (header.php, footer.php, rightPanel.php, home.php) all of which are loaded by a function.
<?php
require_once(realpath(dirname(__FILE__) . "/../config.php"));
function renderLayoutWithContentFile($contentFile, $variables = array())
{
$contentFileFullPath = TEMPLATES_PATH . "/" . $contentFile;
// making sure passed in variables are in scope of the template
// each key in the $variables array will become a variable
if (count($variables) > 0) {
foreach ($variables as $key => $value) {
if (strlen($key) > 0) {
${$key} = $value;
}
}
}
require_once(TEMPLATES_PATH . "/header.php");
echo "<div id=\"container\">\n"
. "\t<div id=\"content\">\n";
if (file_exists($contentFileFullPath)) {
require_once($contentFileFullPath);
} else {
require_once(TEMPLATES_PATH . "/error.php");
}
// close content div
echo "\t</div>\n";
require_once(TEMPLATES_PATH . "/rightPanel.php");
// close container div
echo "</div>\n";
require_once(TEMPLATES_PATH . "/footer.php");
}
?>
in index.php
<?php
require_once(realpath(dirname(__FILE__) . "/resources/config.php"));
require_once(LIBRARY_PATH . "/templateFunctions.php");
require_once(LIBRARY_PATH . "/dealBuilderFunctions.php");
$setInIndexDotPhp = "Hey! I was set in the index.php file.";
// Must pass in variables (as an array) to use in template
$variables = array(
'setInIndexDotPhp' => $setInIndexDotPhp,
);
renderLayoutWithContentFile("home.php", $variables);
?>
so now what if I want to load a different set of content into the template, I have figured I can do this to change the variable in the template page and input some logic to get it to check for a change like below:
<?php
require_once(realpath(dirname(__FILE__) . "/resources/config.php"));
require_once(LIBRARY_PATH . "/templateFunctions.php");
require_once(LIBRARY_PATH . "/dealBuilderFunctions.php");
/*
Now you can handle all your php logic outside of the template
file which makes for very clean code!
*/
$setInIndexDotPhp = "Hey! I was set in the index.php file.";
// Must pass in variables (as an array) to use in template
$variables = array(
'setInIndexDotPhp' => $setInIndexDotPhp,
);
if ($page == "") {
$page = "home";
}
else {
$page=$page;
}
renderLayoutWithContentFile("$page.php", $variables);
?>
URL to get to new page
http://siteurl.com/index.php?page=test
but I keep getting an error Notice: Undefined variable: page in /www/sites/perthdeals/wwwroot/index.php on line 20, how can I get this to work?
I thought the above should just change any variable $page in index.php but if I set it in the page like the error asks I just get the page I defined inside the code, not the one passed in URL, so how should it work?
Alternatively if there are any modern tutorials that will help me understand this I would appreciate it, Thanks.
UPDATE - The Undefined variable: page in /www/sites/perthdeals/wwwroot/index.php on line 20 was a red herring, I was trying to call the variable from the URL wrong, answer marked below, thanks.
parameters from the URL you can get through $_GET, in your case
if ($_GET["page"] == "") {
$page = "home";
}
else {
$page=$_GET["page"];
}

PHP Form variable declaration error [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I am just experimenting with some php at the top of my page to process some form data. It seems to want me to declare the variable even though this does not exist as it will be passed when the form is submitted.
I am getting the following error
Notice: Undefined index: mysubmit in /home/grsim/public_html/age1.php on line 3
<?php
$problem='';
if($_POST['mysubmit']=="Submit Form"){
if($_POST['age']==''){
$problem="The form is blank";
} else {
// do something
$myage = $_POST['age'];
if($myage < 21){
echo "you are a bit young for this";
} else {
echo "you are old enough";
}
}}
Any help would be appreciated as I have not had to declare variables before use. I am using a button with the name of mysubmit.
just change your if line to
if(isset($_POST['mysubmit']) && $_POST['mysubmit']=="Submit Form"){
In first run your variable mysubmit is not set yet.
So you have this message.
Try this code to solve the problem:
$problem='';
$my_submit = isset($_POST['mysubmit']) ? $_POST['mysubmit'] : false;
$myage = isset($_POST['age']) ? $_POST['age'] : false;
if($my_submit == "Submit Form"){
if(empty($myage){
$problem="The form is blank";
} else {
// do something
if($myage < 21){
echo "you are a bit young for this";
} else {
echo "you are old enough";
}
}}
Some servers are set to where it gives the undefined index error when the index you put in $_POST doesn't exist. So do if(isset($_POST['index']) && $_POST['index']=='whatever') { ... }:
<?php
$problem='';
if(isset($_POST['mysubmit']) && $_POST['mysubmit']=="Submit Form"){
if(isset($_POST['age']) && $_POST['age']==''){
$problem="The form is blank";
} else {
// do something
if(isset($_POST['age']) && $_POST['age'] < 21) {
echo "you are a bit young for this";
} else {
echo "you are old enough";
}
}}
This is configurable in the server's setup. It can be set to give you empty string instead of throwing this error.

PHP and MySQL Session

we moved our website to a new server that came with a new IP address. What puzzles me; the website login sessions do not work on the new server but when I change the database IP to the old server they are working.
MySQL Version :
Old server = 5.1.58- Community
New server = 5.1.68 - Community
At first I thought it was a PHP error but I now believe it's not and suspect its MySQL related. Anyone who knows what might have caused this conflict?
Debugging Error :
Notice: A session had already been started - ignoring session_start() in C:\inetpub\wwwroot\gtest\libs\products.php on line 2 Notice: Undefined index: uUserTypeID in C:\inetpub\wwwroot\gtest\admin\index.php on line 50 Notice: Undefined offset: 0 in C:\inetpub\wwwroot\gtest\admin\index.php on line 52 Notice: Undefined offset: 0 in C:\inetpub\wwwroot\gtest\admin\index.php on line 52
Line 50 :
GetUserType($_SESSION['uUserTypeID'], $UserTypeID, $UserTypeDescr, $Active_Tag);
Line 52 :
if (($UserTypeDescr[0] == 'Admin') || ($UserTypeDescr[0] == 'Report'))
Code overview :
<?php
error_reporting(E_ALL);
ini_set('display_errors', True);
session_start();
require '../libs/database.php';
require '../libs/users.php';
require '../libs/products.php';
require '../libs/quotes.php';
require '../libs/common.php';
require 'functions.admin.php';
if (!($_SESSION['uAUID']) > 0)
{
DisplayLoginForm();
}
else
{
**GetUserType($_SESSION['uUserTypeID'], $UserTypeID, $UserTypeDescr, $Active_Tag);**
**if (($UserTypeDescr[0] == 'Admin') || ($UserTypeDescr[0] == 'Report'))**
{
if (isset($_POST['eProdID']) && isset($_POST['eProdGroupID']))
{
$_SESSION['page'] = 'edit_product';
$_SESSION['page_header'] = 'Edit Product';
}
else if (isset($_POST['eProdGroupID']))
{
$_SESSION['page'] = 'edit_product_group';
$_SESSION['page_header'] = 'Edit Product Group';
}
else if (isset($_POST['eAUID']))
{
$_SESSION['page'] = 'edit_user';
$_SESSION['page_header'] = 'Edit User';
Check over your included files/code structure.. A usual cause for this error is:
session_start();
/* Random Code here /*
session_start();
Just the duplicate lines of session_start(); So what I will suggest is to look over your included files/main page(s) that you are receiving this error message on, and check for more than one session_start();

NOTICE Message: Undefined index: mode File: /includes/pages/game/class.ShowBonusPage.php [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
I am having the following error message:
NOTICE
Message: Undefined index: mode
File: /includes/pages/game/class.ShowBonusPage.php
Line: 14
URL: localhost/2moons/game.php?page=bonus
PHP-Version: 5.2.6
Code is :
class ShowBonusPage extends AbstractPage {
function ShowBonusPage() {
global $USER, $PLANET, $LNG, $LANG, $db, $ressoucre, $reslist;
$PlanetRess = new ResourceUpdate();
$PlanetRess->CalcResource();
$PlanetRess->SavePlanetToDB();
$template = new template();
$Mode = $_GET['mode'];
$darkmatter = $USER['darkmatter'];
$tecno = $USER['b_tech_planet'];
$minas = $PLANET['b_building_id'];
$buster = $PLANET['buster_tech'];
$metal = $PLANET['metal'];
$crystal = $PLANET['crystal'];
$deuterium = $PLANET['deuterium'];
The error line is:
$Mode = $_GET['mode'];
Can anyone help me with this
You should user GET and POST in this manner:
$Mode='';
if(isset($_GET['mode'])){
$Mode = $_GET['mode'];
}

Categories