JamesKoash Posted August 27, 2013 Share Posted August 27, 2013 Today doesn't seem to be my day. Having problem after problem. Haha. This time my nested IF statements don't seem to be working. If the name has been input I want to go to the next page of the form, if not I want to throw up an error message. Unfortunately this code seems to call the header() function regardless of whether the form is filled or not. if (isset($_POST['submit'])) { if (isset($_POST['name'])) { $_SESSION['name'] = $_POST['name']; header('Location: page_2.php'); } if (!isset($_POST['name'])) { $nameerror = "<span class=\"feedback\">Please enter valid name</span>"; $_SESSION['name'] = ""; } }; Also, to save me opening another thread later, I'm trying to call the name on the next PHP page that opens. As it's saving the data as part of a session, I thought it would be a case of just calling it again using the following code: session_start(); $_SESSION['name'] = $name; However, that doesn't seem to work either. Is that because I've gotten the code wrong or is that because my IF statements playing up means the name input is being stored to the session? Thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/281598-nested-if-statements/ Share on other sites More sharing options...
Muddy_Funster Posted August 27, 2013 Share Posted August 27, 2013 when the form is submitted the $_POST array is set - whether there are values in it or not. try empty() for the $_POST['name'] instead Quote Link to comment https://forums.phpfreaks.com/topic/281598-nested-if-statements/#findComment-1446972 Share on other sites More sharing options...
JonnoTheDev Posted August 27, 2013 Share Posted August 27, 2013 (edited) Yeah, you want to be checking that the field is not empty. The isset() function does not do this. You should combine with another function like empty() or if you are using php < version 5.5 use the following <?php if(isset($_POST['submit'])) { if(!isset($_POST['name']) || trim($_POST['name']) == FALSE) { $nameerror = "<span class=\"feedback\">Please enter valid name</span>"; unset($_SESSION['name']); } else { $_SESSION['name'] = $_POST['name']; header('Location:page_2.php'); exit(); } } ?> Edited August 27, 2013 by neil.johnson Quote Link to comment https://forums.phpfreaks.com/topic/281598-nested-if-statements/#findComment-1446978 Share on other sites More sharing options...
Andy-H Posted August 27, 2013 Share Posted August 27, 2013 Also note the exit statement after the header redirect, you should make sure you do this after a header redirect as a matter of good practice, this will stop script execution. You don't need the parentheses on the exit statement if no argument is given as exit is a language construct, so the following are equivalent: exit(); exit; Quote Link to comment https://forums.phpfreaks.com/topic/281598-nested-if-statements/#findComment-1446992 Share on other sites More sharing options...
PaulRyan Posted August 27, 2013 Share Posted August 27, 2013 (edited) You should not rely on the forms submit button to be sent along with the form, you should use something like this <?PHP //### Check form was submitted if($_SERVER['REQUEST_METHOD'] == 'POST') { //### Check to see if the POST value 'name' was sent $name = isset($_POST['name']) ? trim($_POST['name']) : FALSE ; //### IF the name is empty, display error if(empty($name)) { $nameerror = "<span class=\"feedback\">Please enter valid name</span>"; unset($_SESSION['name']); //### ELSE session name is set and re-direct } else { $_SESSION['name'] = $name; header('Location: page_2.php'); exit; } } Edited August 27, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/281598-nested-if-statements/#findComment-1447001 Share on other sites More sharing options...
JamesKoash Posted August 27, 2013 Author Share Posted August 27, 2013 Hi there all, Thanks for your answers. I've tried both variations of code and they both seem to work well Still having issues with the second issue though. As mentioned in my initial post, I'm trying to call the variables posted to the session on other pages of the survey but it does not seem to be working. Here is my code: session_start(); $_SESSION['name'] = $name; Also, when should I close sessions? On the very last page of the survey or is it something that needs to be done at the end of every page, seeing as every page begins with session_start()? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/281598-nested-if-statements/#findComment-1447074 Share on other sites More sharing options...
PaulRyan Posted August 27, 2013 Share Posted August 27, 2013 (edited) When ever you are using sessions, you need to start it before ANYTHING else just after the opening PHP tags, example: <?PHP session_start(); //### Other code here You apply that to every page that requires sessions to be used. You don't close a session, you destroy it, using the following (personal preference) $_SESSION = array(); session_destroy(); Edited August 27, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/281598-nested-if-statements/#findComment-1447077 Share on other sites More sharing options...
Solution JamesKoash Posted August 27, 2013 Author Solution Share Posted August 27, 2013 Thank you all for your help - I've learnt a lot from you all PS: I worked out why $_SESSION['name'] = $name; wasn't working when I later used echo/print to display the variable. It should have been $name = $_SESSION['name']; instead. Oops! Quote Link to comment https://forums.phpfreaks.com/topic/281598-nested-if-statements/#findComment-1447081 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.