Page 1 of 1

How to Refill an HTML Form ?

Posted: Mon Jan 22, 2007 5:06 am
by waqas_punjabian
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


hi all,

i am submitting a registration form's data to another php file using post method,
Now if some error occurs, i want to redirect user back to the page from where he 
came, with an error reporting.

How can i refill the data in that form fields ?


On second page i 've used following code :

Code: Select all

//if error then do following
send_back('Create_New_Account.php');

function send_back($action)
{
  print("<form name='frmBack' id='frmBack' action='".$action."' method='post'>");
  foreach($_POST as $key => $value)
  {
	print "	<input id='".$key."' name='".$key."' type='hidden' value='".$value."'>";
  }
  print("</form>
			<script>
			document.frmBack.submit();
			</script>");
}
It always gives me this error :

Code: Select all

document.frmBack has no properties
. IT doesn't prints hidden input field, where as always prints other data.

Please tell me how can i submit all the fields of this form to the previous page?

Note: on previous page i 'll use $_POST['var_name'] for data check.

regards
WAQAS


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jan 22, 2007 5:31 am
by CoderGoblin
The normal method is to process the result on the first page, then redirect if successful.

Code: Select all

<?php
if (!empty($_POST['submitted'])) {
  // validate
  if ($all_valid) {
    header('Location:http://www.site.com/nextpage.php');
    exit;
  }
} else {
  //Set/Show errors
}

// Whatever
?>
The alternative is to store the values, possibly in a session variable when processing the second form and use that to repopulate the first.

The first method is better as it keeps the validation with the form it is validating.

Hope that helps

Posted: Mon Jan 22, 2007 7:03 am
by Ollie Saunders
You can only access and manipulate the DOM after the page has loaded.

Code: Select all

window.onload = function()
{
    document.frmBack.submit(); 
}
But you'll laugh when you see what that does :)