Page 1 of 1

Form Validation w/ submit to new page

Posted: Sun May 14, 2006 9:06 pm
by AZRoushStang
Hey all,

I've been doing a lot of searching for an ability to validate a form. I have found several examples that have the php form validation with the php code on the same page as the form, which is what I would like. However, ALL of these examples simply post back to themselves wether an error is found or not...What I need to be able to do is to go to post the form data to a new page if all of the form data is valid. Does this make senes? Simply put "Bad form data ==> self with error message and sticky form, Good form data ==> post form data to new page"

Below is one of the examples that I have found, but that always posts back to itself, could somebody please help me with this issue?

Code: Select all

<!--BEGIN CODE-->

<html>
<body>

<h3>PHP User Registration Form Example</h3>

<?
// only validate form when form is submitted
if(isset($submit_button)){
	$error_msg='';
	if(trim($username_input)=='' || strlen(trim($username_input)) < 6 || strlen(trim($username_input)) > 15) {
		$error_msg.="Please enter a username between 6 to 15 characters long<br>";
	}
	if(trim($password_input)=='' || strlen(trim($password_input)) < 4) {
		$error_msg.="Please enter a password at least 4 characters long<br>";
	}
	if(trim($email_input)=='') {
		$error_msg.="Please enter an email<br>";
	} else {
		// check if email is a valid address in this format username@domain.com
		if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $email_input)) $error_msg.="Please enter a valid email address<br>";
	}

	// display error message if any, if not, proceed to other processing
	if($error_msg==''){
		// other process here
	} else {
		echo "<font color=red>$error_msg</font>";
	}
}
?>

<form method="POST" action="registration.php">
<table border="1" cellpadding="7" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
  <tr>
    <td width="16%" align="right">First Name</td>
    <td width="84%">
    <input type="text" name="first_name_input" size="20" value="<? echo $first_name_input; ?>"></td>
  </tr>
  <tr>
    <td width="16%" align="right">Last Name</td>
    <td width="84%">
    <input type="text" name="last_name_input" size="20" value="<? echo $last_name_input; ?>"></td>
  </tr>
  <tr>
    <td width="16%" align="right">Username</td>
    <td width="84%">
    <input type="text" name="username_input" size="20" value="<? echo $username_input; ?>"> (between
    6 to 15 characters)</td>
  </tr>
  <tr>
    <td width="16%" align="right">Password</td>
    <td width="84%">
    <input type="password" name="password_input" size="20" value="<? echo $password_input; ?>">
    (must be at least 4 characters)</td>
  </tr>
  <tr>
    <td width="16%" align="right">Email</td>
    <td width="84%">
    <input type="text" name="email_input" size="20" value="<? echo $email_input; ?>"></td>
  </tr>
  <tr>
    <td width="16%" align="right"> </td>
    <td width="84%">
    <input type="submit" value="   Register   " name="submit_button"></td>
  </tr>
</table>
</form>

</body>
</html>

<!--END CODE-->
Burrito: Please use

Code: Select all

tags when [url=http://forums.devnetwork.net/viewtopic.php?t=21171]posting code in the forum[/url].[/size]

Posted: Sun May 14, 2006 10:04 pm
by flann
you could validate it and insert it on the same page, and then after you insert it, use header ("Location: new_page.php"); to redirect to a new page. Just make sure you do all this before any header information is sent tot the browser.

Posted: Sun May 14, 2006 10:28 pm
by AZRoushStang
So does that mean putting the php block (ie <? ... ?>), before the html block (<html> ... </html>)?

Also, so is that pretty much the only way to have mutiple submit to pages? But, I do like your idea, i guess that would work...thanks~

Sorry, i'm a little new to php for server side programming...