ECHO not showing in HTML from PHP [duplicate] - php

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 6 years ago.
For some reason the h1 only says Welcome to Arma 3 Life Highway Patrol's
Any ideas?
<?php
//This file will contain the page name and the credits!
function thisPage($page){
if ($page == 'sops.php'){
$Npage = 'SOPs';
return $Npage;
}elseif ($page == 'changelog.php'){
$Npage = 'Website Changelog';
return $Npage;
}elseif ($page == 'ftodocuments.php'){
$Npage = 'FTO Documents';
return $Npage;
}elseif ($page == 'index.php'){
$Npage = 'Homepage';
return $Npage;
}elseif ($page == 'login.php'){
$Npage = 'FTO Login Page';
return $Npage;
}elseif ($page == 'socialstuff.php'){
$NPage = 'Social Stuff';
return $Npage;
}else{
return false;
}
}
?>
<?php
$findPage = (basename($_SERVER['PHP_SELF']));
$thisPageName = thisPage($findPage);
?>
<h1>Welcome to the Arma 3 Life Highway Patrol's <?php echo $thisPageName ?></h1>
<p> Website created by Austin Sharp and assisted by Greyson Henley.</p>

the problem is your variables. You have two different variables:
$NPage = 'Social Stuff';
return $Npage;
$NPage and $Npage they do not match.

You can use this $findPage = basename($_SERVER['SCRIPT_FILENAME']);
It will give the filename and print the proper output.

I actually just tested the code on MAMP and it worked as it should (saved the file as index.php). What server are you testing this on?
Shot in the dark, but try changing $_SERVER['PHP_SELF'] to $HTTP_SERVER_VARS['PHP_SELF'].
Maybe you could try phpinfo() to see if global php variables are empty.

Related

PHP fixing undefined error with echo instead? But does not work [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 2 years ago.
I am inserting and updating some values through a form with radioboxes and textarea.
Basically the query works fine but if user does not select any radio boxes or leaves the text area empty, I want an error message to pop up, instead of real php errors of undefined.... blah blah.
Sorry for this weird layout, but this text editor is super problematic with editing..
$var1 = $_POST['var_1'];
$var2 = $_POST['var_2'];
$var3 = $_POST['var_3'];
if(isset($_POST['button_name'])) {
if(query1 && query2) {
echo "Successfully inserted and updated"</h3>";
} elseif(empty($var1) && empty($var2) && empty($var3)) {
echo "not selected. Please Try Again"</h3>";
} else {
echo "failed to sent cause of query issue";
}
} else {
echo "nothing was selected";
}
First check if the variables are defined, then set them to their values:
$var1, $var2, $var3;
if(issset($_POST['var_1']) && isset($_POST['var_2']) && isset($_POST['var_3']) && isset($_POST['button_name'])) {
$var1 = $_POST['var_1'];
$var2 = $_POST['var_2'];
$var3 = $_POST['var_3'];
} else {
echo "Your custom error message";
}
Alternatively, you can use try and catch:
try {
$var1 = $_POST['var_1'];
$var2 = $_POST['var_2'];
$var3 = $_POST['var_3'];
$_POST['button_name'];
} catch {
echo "Your custom error message";
}

php error undefined index with 2 variable [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 5 years ago.
anyone can help me?
i used this code for managing pages
and it's get
undefined index : page
undefined index : aksi
<?php
$page = $_GET['page'];
$aksi = $_GET['aksi'];
if($page == "") {
if($aksi == "") {
include "page/dashboard.php";
}
} elseif($page == "dashboard") {
if($aksi == "") {
include "page/dashboard.php";
}
}elseif($page == "masuk") {
if($aksi == "") {
include "page/kas_masuk/masuk.php";
}
if($aksi == "hapus") {
include "page/kas_masuk/hapus.php";
}
}
?>
Use isset() function like below,
UPDATE
To get value of $_GET[] use below
$page = (isset($_GET['page'])?$_GET['page']:'');
$aksi = (isset($_GET['aksi'])?$_GET['aksi']:'');
Now we will get value if $_GET[] have value otherwise empty without returning any error or warning

getting an "undefined variable" on isset [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 6 years ago.
So i've got a rather complex bit of code that i'm trying to make work (actually; it works fine on my test server but not so much my live server. so i'm trying to make it rework) -- it keeps telling me that a variable i'm asking it to check with isset is not a defined variable. I'm probably over thinking this whole method and there's probably a simplier way. but i need you guys to bash me over the head with it apparently
include('bootstra.php');
include('includes/parts/head.php');
if(!isset($_SESSION['user']['uid']))
{
echo $html;
include('includes/parts/login.php');
if(isset($_GET['mode']))
{
$file = $_GET['mode'];
$path = "includes/parts/{$file}.php";
if (file_exists($path)) {
require_once($path);
} else {
die("The Page REquested could not be found!");
}
}
else
{
include('includes/parts/frontpage.php');
}
}
else
{
if(!isset($_SESSION['x']) || !isset($_SESSION['y']))
{
if(isset($_GET['x']) && isset($_GET['y']))
{
$_SESSION['x'] = $_GET['x'];
$_SESSION['y'] = $_GET['y'];
}
else
{
$_SESSION['x'] = 0;
$_SESSION['y'] = 0;
}
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
if($_SESSION['x'] != $_GET['x'] || $_SESSION['y'] != $_GET['y'] )
{
header("location:index.php?x={$_SESSION['x']}&y={$_SESSION['y']}");
}
else
{
echo $html;
echo $base->displayMap($_GET['x'], $_GET['y']);
include('includes/context_menu.php');
}
}
}
Here is the exact error:
Notice: Undefined index: x in /home/****/public_html/reddactgame/index.php on line 27
Change it
<pre>
if( !isset($_SESSION['x']) || !isset($_SESSION['y']) )
</pre>
to
<pre>
if( !( isset($_SESSION['x']) && isset($_SESSION['y']) ) )
</pre>
There is no error, that is a Warning...
It is saying that $_SESSION['x'] was never setted, so, when you do that isset it tells you that it was never declared.
Example:
This gives you the same warning
empty($x);
This does not give you warning
$x = 0;
empty($x);
EDIT
I see this is a common question, so here is a better explanation.

var undefined getting value $ _GET ['page']

I want paging is a content of a news blog. Everything works correctly, the page content is successful. But I get an error screen displays PHP:
Notice: Undefined index: page in C:\wamp\www\index.php on line 146
code with the line that gives the error:
$maxreg = 1;
$pag = $_GET['page'];
if (!isset($pag) || empty($pag)){
$min = 0;
$pag = 1;
}else{
if($pag == 1){
$min = 0;
}else{
$min = $maxreg * $pag;
$min = $min - $maxreg;
}
}
include("js/class.AutoPagination.php");
$obj = new AutoPagination(contar_contenido(), $pag);
mostrar_contenido($min,$maxreg);
echo $obj->_paginateDetails();
The line gives the error is this:
$ page = $ _GET ['page'];
The first page by default index.php and contains no var in the url.
I do not understand why if fails below by a conditional var I determine if that has content or not.
Should not show any error php, could someone give me a solution? Thanks
You don't need to do
$pag = $_GET['page'];
Check if $_GET['page'] is set first:
if(!isset($_GET['page']) || empty($_GET['page'])) {
$min = 0;
$pag = 1;
}else{
if($pag == 1){
$min = 0;
}else{
$min = $maxreg * $pag;
$min = $min - $maxreg;
}
}
I quote;
Declare your variables. Or use isset() to check if they are declared before referencing them;
PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"
I found the solution, I've built so, assigning a value to the variable empty get in if it is indefinite.
Maybe it's a very elegant solution, but it works perfect paging.
if(!isset($_GET['page']) || empty($_GET['page'])) {
$_GET['page']="";
}
$maxreg = 2;
$min = 0;
$pag = $_GET['page'];
I think the second conditional left over.
I leave it here in case anyone has the same problem. This way you can fix it.

PHP for loop variable [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.
I have made this for loop to get a variable amount of columns selected from the database. As I'm running this, I get an error saying :
Notice: Undefined variable: kolom_1
Notice: Undefined variable: kolom_2
Notice: Undefined variable: kolom_3
Notice: Undefined variable: kolom_4
Notice: Undefined variable: kolom_5
Notice: Undefined variable: kolom_6
But I have it all placed in a for loop, why does he not recognize them? I am not getting what I'm doing wrong.
function lijst_ophalen($data, $from){
$totaal = count($data);
for ($i=1; $i<$totaal; $i++){
$kolom_[$i] = $this->mysqli->real_escape_string($data['kolom_' . $i . '']);
if($kolom_[$i]!="") $kolom_[$i] = "{$kolom_[$i]},"; else $kolom_[$i]="";
if($kolom_[$i]==$totaal) $kolom_[$i] = "{$kolom_[$i]}";
}
$from_table = "";
$categorie = "";
if($from == "bv"){
$from_table = "klanten_algemene_gegevens_bv";
$categorie = "";
}
if(($from == "1manszaak") || ($from == "vof")){
$from_table = "klanten_algemene_gegevens_vof_1manszaak";
if($from == "1manszaak"){
$categorie = "1manszaak";
}
if($from == "vof"){
$categorie = "vof";
}
$categorie = "WHERE soort_onderneming = '{$categorie}'";
}
if($from == "ib"){
$from_table = "klanten_ib";
$categorie = "";
}
$result = $this->mysqli->query(
<<<EOT
SELECT
{$kolom_1}
{$kolom_2}
{$kolom_3}
{$kolom_4}
{$kolom_5}
{$kolom_6}
FROM {$from_table}
{$categorie}
EOT
);
if($result){
$waardes = array();
while ($row = $result->fetch_assoc()) {
$waardes[]=$row;
}
return $waardes;
}
}
When you are initializing / filling data into the $kolom_ you are creating an' array instead of your (properly) intended series of different variables.
$kolom_[$i] <--- ARRAY
And lower down you are calling a series of variables $kolom_1, which doesn't exist because you created an' array and not a series of variables.
To avoid the error you can simply change your
$kolom_1
$kolom_2 (and so on)
calls into
$kolom_[1]
$kolom_[2] (and so on)
and you should be set to go.

Categories