|
Posted by Pedro Graca on 11/17/06 22:17
Jerim79 wrote:
> Here is the current, non-working code:
>
> <?php
> $FName="";
> $errormsg1="";
>
> if( isset($_POST['FName']){
> $FName=$_POST['FName'];
> }
>
> if ($FName !=""){
> header("Location: write_to_database.php");
> }
> else{
> if ($FName=""){
> $errormsg1="Please enter a first name":
> }
> ?>
> <html>
> <body>
> Registration
> <?php if ($errormsg1 !=""){ echo $errormsg1; } ?>
> <form action="new.htm" method="POST">
> First Name: <input type="text" name="FName">
> <br />
> <input type="submit" name="submit" value="Submit">
> </form>
> </body>
> </html>
Here's how I'd do it:
<?php
########
# initialize variables for user input and error messages
$FName = false;
$FName_error = false;
$LName = false;
$LName_error = false;
# initialize an error count variable
$error_count = 0;
########
# Check if the page was accessed with a POST
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$FName = isset($_POST['FName']) ? $_POST['FName'] : '';
if (!name_is_valid($FName)) {
$FName_error = 'Please enter a valid First Name.';
++$error_count;
}
$LName = isset($_POST['LName']) ? $_POST['LName'] : '';
if (!name_is_valid($LName)) {
$LName_error = 'Please enter a valid Last Name.';
++$error_count;
}
if (!$error_count) {
########
# If the script reaches here, there have been no errors detected in
# the user data, so we can save to the database, send mail, display
# a 'thank you' page, and/or redirect
// mysql_query("insert ...");
// exit('Thank you');
}
}
########
# When we get here after a GET $error_count will be 0 (zero).
# When we get here after a POST $error_count will *NOT* be 0
# because in the if (!$error_count) above we always exit().
# So
if ($error_count) {
echo 'There were errors in your submission. Please review and re-submit';
}
echo '<form action="', $_SERVER['PHP_SELF], '" method="post">';
echo 'First Name: <input type="text" value="', $FName, '">';
if ($FName_error) {
echo ' <span class="error">', $FName_error, '</span>';
}
echo "<br>\n";
echo 'Last Name: <input type="text" value="', $LName, '">';
if ($LName_error) {
echo ' <span class="error">', $LName_error, '</span>';
}
echo "<br>\n";
echo '<input type="submit">';
echo '</form>';
HTML;
Happy Coding :)
--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
[Back to original message]
|