nehrav Posted October 15, 2009 Share Posted October 15, 2009 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 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?? Link to comment https://forums.phpfreaks.com/topic/177767-solved-undefined-variable-when-using-_serverphp_self/ Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 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. Link to comment https://forums.phpfreaks.com/topic/177767-solved-undefined-variable-when-using-_serverphp_self/#findComment-937359 Share on other sites More sharing options...
nehrav Posted October 15, 2009 Author Share Posted October 15, 2009 And how I can store that value in session?? writing session_start(); on second.php would be enough?? Link to comment https://forums.phpfreaks.com/topic/177767-solved-undefined-variable-when-using-_serverphp_self/#findComment-937386 Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 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. Link to comment https://forums.phpfreaks.com/topic/177767-solved-undefined-variable-when-using-_serverphp_self/#findComment-937400 Share on other sites More sharing options...
nehrav Posted October 15, 2009 Author Share Posted October 15, 2009 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... Link to comment https://forums.phpfreaks.com/topic/177767-solved-undefined-variable-when-using-_serverphp_self/#findComment-937435 Share on other sites More sharing options...
cags Posted October 15, 2009 Share Posted October 15, 2009 // 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']; Link to comment https://forums.phpfreaks.com/topic/177767-solved-undefined-variable-when-using-_serverphp_self/#findComment-937438 Share on other sites More sharing options...
nehrav Posted October 15, 2009 Author Share Posted October 15, 2009 // 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.......... Link to comment https://forums.phpfreaks.com/topic/177767-solved-undefined-variable-when-using-_serverphp_self/#findComment-937450 Share on other sites More sharing options...
nehrav Posted October 16, 2009 Author Share Posted October 16, 2009 // 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.... Link to comment https://forums.phpfreaks.com/topic/177767-solved-undefined-variable-when-using-_serverphp_self/#findComment-937922 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.