PDA

View Full Version : Error reporting from input field, save values


error_22
11-06-2006, 08:12 PM
Hello!

I have a question about error reporting (if thats what its called).

Lets say I have a form:

<form action="save.php" method="post">
<input type="text" name="hello" />
<input type="submit" value="save" />
</form>


The value from the hello field is then sent to save.php where a script checks if that value already exists. if it does, then I want save.php to send the visitor back to the html form. i want there to be an error message (your name is already taken) and the value you put in should be displayed in the input field.

Is there an easy way to achieve this?

Thanks in advance
Niklas

error_22
11-08-2006, 06:43 PM
Bump

nico_swd
11-08-2006, 06:54 PM
You can store the errors in sessions if you don't post the data to the same page.



session_start();

if (/* username exists */)
{
$_SESSION['errors'][] = 'Username taken';
}

// Go back to previous page and...

if (is_array($_SESSION['errors']))
{
echo 'Following errors ocurred: <br />';

foreach ($_SESSION['errors'] AS $error)
{
echo '- '. $error .'<br />'. "\n";
}
}

error_22
11-09-2006, 09:12 AM
Excellent!

One questin, what is the best way to "Go back to previous page"?

I tried using header() but it gives me errors since a header has already been sent.

nico_swd
11-09-2006, 09:48 AM
You can use header('Location: ..., you just can't output anything before sending them. Neither white spaces nor HTML.

error_22
11-09-2006, 01:01 PM
and what if i do have hml before? is it impossible then? do i have to re-arrange in the document?

thanks for all the help!

nico_swd
11-09-2006, 01:09 PM
You can redirect with HTML then.

Echo this instead.

<meta http-equiv="refresh" content="0;URL=http://url.com" />