This may seem like a dumb question...well, it probably is.

I am trying to write code for form validation. I wrote the code to check the information and then if everything looks okay, to use header ("Location....... and send them to the next page, but of course I get the big Warning Headers Already Sent information which makes me think I've done all of this the wrong way.

Can anyone give me advice on how to set this up. What I did was make the html form with action linking to the code I made in Notepad. Is there a better way? What am I missing here?

Thanks,
~Amy

Recommended Answers

All 8 Replies

you can't have anything output to the browser before a header call, not even a "\n" line break. i store my html in a variable and echo at the end of my script to eliminate this problem.

Well, my code checks the information entered into the form. Then if everything is okay, it needs to take it to another page with a form on it and the information from the first form is stored in the session.

Here is a small copy of my code to show you what I'm talking about:

<?php
session_start();

include ("databaseinfo.inc");

mysql_connect ($host, $user, $password, $database)
or die ("No Connection");
	      
//Form validation:

if ((!empty($_POST['logname']))
&& (strlen($_POST['logname']) >6)
&& (strlen($_POST['logname']) <20))
{
$logname=$_POST['logname'];
$logname=htmlspecialchars($logname);
}
else
{
echo 'You forgot to enter a valid entry=User Name!';
}

if ((!empty($_POST['password']))
&& (strlen($_POST['password']) >6)
&& (strlen($_POST['password']) <20))
{
$password=$_POST['pass'];
$password=htmlspecialchars($password);
}
else
{
echo 'You forgot to enter a valid entry=Password!';
}

if (($_POST['password'])!=$_POST['cpassword'])
{
echo 'You need to Confirm your Password!';
}

if ($logname==htmlspecialchars($logname) && 
$password==htmlspecialchars($password) &&
$_POST['password']==$_POST['cpassword']
{
header
("Location:http://www.thenextpage.php");
}
else
{echo "Cannot continue unless correct information is provided.";   
}
?>

How can I do this successfully?
Thanks!
~Amy

i made a few minor changes to show you how to fix this problem.

<?php
session_start();

include ("databaseinfo.inc");

mysql_connect ($host, $user, $password, $database)
or die ("No Connection");
	      
//Form validation:

$msg = '';

if ((!empty($_POST['logname']))
&& (strlen($_POST['logname']) >6)
&& (strlen($_POST['logname']) <20))
{
$logname=$_POST['logname'];
$logname=htmlspecialchars($logname);
}
else
{
$msg .= 'You forgot to enter a valid entry=User Name!';
}

if ((!empty($_POST['password']))
&& (strlen($_POST['password']) >6)
&& (strlen($_POST['password']) <20))
{
$password=$_POST['pass'];
$password=htmlspecialchars($password);
}
else
{
$msg .= 'You forgot to enter a valid entry=Password!';
}

if (($_POST['password'])!=$_POST['cpassword'])
{
$msg .= 'You need to Confirm your Password!';
}

if ($logname==htmlspecialchars($logname) && 
$password==htmlspecialchars($password) &&
$_POST['password']==$_POST['cpassword']
{
header("Location:http://www.thenextpage.php");
exit;
}
else
{
$msg .= "Cannot continue unless correct information is provided.";   
}

echo $msg;

?>

in this page, make sure there isn't any html or line breaks above this code, or the redirect will not work.

use ob_start() function at the starting of the page it is the output buffer

Hmmmm....kkeith29, I did as you said, but now when I submit the form to be checked, it doesn't notify me of all the errors. Instead it just goes to the next page. I need it to stop the users and tell them that they entered something wrong. What am I doing wrong?

Also, I have tried to use the ob_start and ob_end_flush and it does the same thing.

Hmmmmm.....any ideas?

Thanks!
~Amy

I guess what I'm confused about is the php in the header???

Okay, another question...when I do enter everything correctly, it does take me to the next page with no problem. Can anyone tell me why this is? I don't want this error to pop up when someone incorrectly puts something in.

Okay, this is strange. I changed the code to make the errors variables and somehow it started working. Weird

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.