How to Refill an HTML Form ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
waqas_punjabian
Forum Commoner
Posts: 67
Joined: Wed Aug 10, 2005 9:53 am

How to Refill an HTML Form ?

Post 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]
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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 :)
Post Reply