Page 1 of 1

HELP needed with if statement

Posted: Thu Apr 05, 2012 12:37 am
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

Re: HELP needed with if statement

Posted: Thu Apr 05, 2012 1:54 am
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.

Re: HELP needed with if statement

Posted: Thu Apr 05, 2012 9:42 pm
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.