...

View Full Version : Best way to handle <FORM> result page ?



Emperor
07-06-2005, 07:41 AM
Hi guys,

I have the general setup:

MyForm.htm // the HTML form
MyForm.php // the PHP result script

Everything works fine. I do however want to handle the case where someone goes to MySite.com/MyForm.php without getting there from the MyForm.htm page.

I tried a hidden field but I get an undefined index error if they don't go from the form page.

What's the best way to handle this situation ? I'm looking for something clean and not too complicated.

Thanks.

Serex
07-06-2005, 07:51 AM
on your myform.php file surround all your code in


if ($_POST['Submit'])
{
// all your code here
}
else
{
echo "error you cannot view this page";
}

if ($_POST['Submit']) just checks to see if your submit button was pressed.. there are many other ways to do it. im sure more people will post

Emperor
07-06-2005, 05:43 PM
I get this:

PHP Notice: Undefined index: Submit in ...

Now it doesn't work at all, even if I press Submit.

Fou-Lu
07-06-2005, 06:05 PM
Try something like so:


<?php

if (!isset($_POST['submit']) OR empty($_POST['submit']))
{
echo 'You cannot view this page.';
exit;
}

/* processing code here */


Just name your submit input 'submit'

NancyJ
07-06-2005, 06:20 PM
rather than just putting an error message, personally I would redirect to MyForm.htm



<?php

if (!isset($_POST['submit']) OR empty($_POST['submit']))
{
header("Location: http://" . $_SERVER['HTTP_HOST']
. dirname($_SERVER['PHP_SELF']). "/" . "MyForm.htm");
}


Just better for a usability point of view IMO, error messages should be avoided if at all possible

Emperor
07-06-2005, 07:07 PM
Thanks everyone.

I like redirecting also, I just had the 'if' test messed up. It works great now, I'm doing it like Nancy recommended.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum