HELP needed with if statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RossBryan
Forum Newbie
Posts: 19
Joined: Fri Aug 19, 2011 3:03 pm

HELP needed with if statement

Post by RossBryan »

Does anybody have any idea why the following code does not execute correctly. I want the fields in the form to be checked for content on submission but the if condition is just passed even when there isn't any content held in the fields. I'm confused:

Code: Select all

<?php
include 'initialize.php';

if (!(isset($_SESSION['user_id'])&& $_SESSION['user_id']!=''))//if the following is false- session not been assigned a valid user value/is blank then...
{
	header('Location:index.php');
	end();	
}
include 'template/top.php';
?>

<h3>Create Folder</h3>

<?php 
if (isset($_POST['folder_name'], $_POST['folder _description']))
{
	$folder_name = $_POST['folder_name'];
	$folder_description = $_POST['folder_description'];

	if (empty($folder_name) || empty($folder_description))
	{
		echo 'Folder name and description required';
	}
	else
	{
		create_folder($folder_name, $folder_description);
		header('Location: uploads.php');
		exit();	
	}
}
?>

<form action='' method='POST'>
<p>Name:<br/><input type='text' name'folder_name' maxlength'55'/></p>
<p>Description:<br/><textarea name='folder_description' rows='6' cols='35' maxlength'255'></textarea></p>
<p><input type='submit' value='Create' /></p>

<?php
include 'template/bottom.php';
?>
any help will be much appreciated...thanks
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: HELP needed with if statement

Post by social_experiment »

isset() only checks if the variables are set, so they can be empty but the first section of the checking will be passed even if no values are present in them.

A common format for checking form submission is to test if the submit button has been set and once that has been confirmed start testing the fields from the form.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
RossBryan
Forum Newbie
Posts: 19
Joined: Fri Aug 19, 2011 3:03 pm

Re: HELP needed with if statement

Post by RossBryan »

Sorted it, was the silliest thing, turns out i was looking at all the wrong places. Never included the = symbol when when assigning the form name field:

Code: Select all

name'folder_name'
Thanks for everyones help anyway.
Post Reply