Jump to content

Nested if statements


Go to solution Solved by JamesKoash,

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/281598-nested-if-statements/
Share on other sites

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 by neil.johnson

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;

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 by PaulRyan

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.

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 by PaulRyan
  • Solution

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.