Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Directing to another page after submit

Status
Not open for further replies.

tshey

Technical User
Dec 28, 2005
78
AU
I am trying to show a confirm page once the win form has been submitted. But is just redirects back to itself with a ? after it.

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction = "?" . htmlentities($_SERVER['QUERY_STRING']);
 
i've seen a number of posts from you on what appears to be the same script. hope you're not getting too frustrated!

i don't think it's advisable to test the query string as a a way of checking whether the form has been correctly processed.

the usual method for a self-processing page is:
Code:
1. check whether the form is submitted
   A. if it has been submitted, validate the form.
      (i) if validated (and not previously entered):
          (z) save record in database
          (y) display some comfy message
      (ii) if not validated:
           (z) redisplay form with previously entered values
           (y) feedback to the user where he has gone wrong
  B.  if not submitted - display blank form

to my mind the best way of separating out this logic in real code is by setting everything in different functions:

Code:
<?
$submitted = "";
test_state();

function test_state()
{
if (isset($_POST['submit'])):
 global ($submitted);
 $submitted = true;
 validate_form();
else:
 display_form();
endif;
}

function validate_form()
{
$validity = false;
//test validity and set to true if ok

if (!$validity):
display_form($msg); //put feedback in $msg
else:
save_form_date();
}

function save_form_data
{
  //trim and clean all incoming data
  // write to the database
  if (!$result):
  display_form("Database Error");
  else:
  display_comfy_message();
  endif;
}

function display_comfy_message()
{
 echo "Your data has been properly entered into the database";

/*
header("Location:newpage.php"); //and set newpage.php to contain whatever you want.
this second method should be coupled with unsetting $_POST and a unique identifier in the form to avoid duplicate db entried being created by the back button + refresh
*/
}

function display_form($msg="")
{
echo $msg;
global ($submitted);
if ($submitted):
 extract ($_POST);
endif;
$erlevel = error_reporting();
error_reporting (0); //suppress errors to avoid notices for unfilled variables.
/*
now insert form elements using the notation
<input name="element_name" type="text" value=<?=$element_name?> /> 
of course this only works for text, hidden, password and textarea input types. you should also do similar things for radio groups, checkboxes and selects of course.  these are easy to do once you get your head around the concept.  check out the pear class HTML_Quickform which handles all this for you
*/
error_reporting ($erlevel);
}

please forgive any parse errors - i have typed the above straight into the tek-tips site.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top