Jump to content

[SOLVED] Undefined variable when using $_SERVER['PHP_SELF']


nehrav

Recommended Posts

Hi guyz, Plz suggest me something...

On first.php I have one input field NAME, and on posting the form it moves to "second.php" showing the entered value of input field using $_POST, everything works fine....

 

But on second page, I have 'n' number of results coming from database. So to control their display,

I use pagination.....< [1] 2, 3 >  its like only 10 records in one shot

 

when I click on 2, to see the records from 11-20

 

URL get change to http://localhost/test/second.php?pg=2

from http://localhost/test/second.php

and start displaying message Notice : Undefined variable NAME on line 18, whereas earlier it was defined for the page  :confused:

 

I m using <a href='{$_SERVER['PHP_SELF']}?pg=$x'>$x</a> for paging

 

What is the possible solution and why its not recognizing the posted variable now??

Undefined variable, or more commonly in the situation you have Undefined Index basically means your refering to a variable or item in an array that doesn't exist. When you click submit on first.php the values of that form are POST'ed to second.php, as soon as you click on the link second.php?pg=2 you are re-requesting the page so the values in the $_POST array are lost. You will need to persist any value in the $_POST array that you require, by either adding it on the end of the url in the same manner as pg=2 or by storing the information in a session.

To use sessions, any page requiring access to the $_SESSION array will need session_start() at the top of the page. After you first arrive at second.php copy any values you need to persist from the $_POST array into the $_SESSION array, and then you use the $_SESSION array to access the value.

On second.php, earlier it was,

<?php echo $_POST["name"]; ?>

now it should?

<?php
session_start();
echo $_SESSION['name'];
?>

although, I haven't use any session array on first page yet, its just session_start(); on first line of first page...

// if form is being submitted
if(isset($_POST['name'])) {
   // copy value from post array to session to persist it
   $_SESSION['name'] = $_POST['name'];
}

// use value
echo $_SESSION['name'];

 

Thanks Cags, I will try that soon.......... :-*

// if form is being submitted
if(isset($_POST['name'])) {
   // copy value from post array to session to persist it
   $_SESSION['name'] = $_POST['name'];
}

// use value
echo $_SESSION['name'];

 

It's solved now........thanks....

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.