Re: Keeping existing data in textarea's

From: Date: Tue, 04 Mar 2003 12:38:33 +0000
Subject: Re: Keeping existing data in textarea's
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-138085@lists.php.net to get a copy of this message
Here's a quick sample of how you can structure a form, and all it's validation, and all it's error messages, and thankyou notes all onto one script. I haven't included every little line and data check that I should (otherwise you'd have to pay me :P), but it works. The beauty of keeping it all in one script is that you can echo the POSTed variables back out into the form, with an error message. This should be a suitable framework for you to further develop into your own application. <? // uncomment next 3 lines if register globals off // $name = $_POST['name']; // $email = $_POST['email']; // $action = $_GET['action']; if(empty($action)) { $show_form = 1; } else { $show_form = 0; } if($action == "validate") { $error = ''; // check for empty vars if(empty($name)) { $error .= "- you didn't enter your name<br />"; } if(empty($email)) { $error .= "- you didn't enter your email<br />"; } // perform any other validation as needed if($error) { $show_form = 1; } else { // insert into database, send email, redirect or whatever header("Location: {$_SERVER['PHP_SELF']}?action=thanks"); exit; } } ?> <html> <body> <? if($show_form) { if($error) { echo "{$error}<br />"; } echo "<form action='{$_SERVER['PHP_SELF']}?action=validate' method='POST'>\n"; echo "Name: <input type='text' name='name' value='{$name}' /><br />"; echo "Email: <input type='text' name='email' value='{$email}' /><br />"; echo "<input type='submit' name='submit' value='submit' /><br />"; echo "</form>"; } if($action == "thanks") { echo "thanks!!!"; } ?> </body> </html> Justin French

« previous php.general (#138085) next »