thedoctor Posted January 1, 2009 Share Posted January 1, 2009 Hello all - Happy New Year & season's greetings! I'm a newbie with PHP, so please, be kind. I've been trying to adapt a PHP contact form that verifies the fields before it's posted (eventually) thru a MySQL insert. The issue is that, while I've set the "usrname" length to fail if it's less than/equal to 3 chars, it will still return the error if it's more than that. I'm adapting this script/setup fr http://snipplr.com/view/3498/php-form-validation-and-processing-same-page/ . I've been pulling my hair out for a few days now, so your advice is appreciated! Feel free it you think there's anything I could add! TIA, - The Doctor <?php // http://snipplr.com/view/3498/php-form-validation-and-processing-same-page/ function VerifyForm(&$values, &$errors){ // Do all necessary form verification if (strlen($values['usrname']) <= 3) $errors['usrname'] = '<ul> <li>Requested username is too short</li> </ul>'; elseif (strlen($values['usrname']) > 60) $errors['usrname'] = '<ul> <li>Requested username is too long</li> </ul>'; /* // Needs better checking if (!ereg('.*@.*\..{2,4}', $values['email'])) $errors['email'] = '<ul> <li>E-mail address invalid</li> </ul>'; */ /* if (strlen($values['name']) < 3) $errors['name'] = 'Name too short'; elseif (strlen($values['name']) > 50) $errors['name'] = 'Name too long'; if (strlen($values['text']) == 0) $errors['text'] = 'Text required'; */ return (count($errors) == 0); } function DisplayForm($values, $errors){ // closes @ ln above function ProcessForm ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title><?php include ("config.php"); echo "$appname"; ?>: User Registration</title> <link href="rite.css" rel="stylesheet" type="text/css" title="default" > <style type="text/css" media="screen, print"><!-- @import url(default.css) screen; --></style> </head> <body> <div class="wrapper"> <h1><?php include ("config.php"); echo "$appname"; ?></h1> <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post"> <fieldset> <legend>User Registeration</legend> <dl> <dt> <?php if (count($errors) > 0) echo "<span class='error'>Error</span>"; ?> </dt> <dd> <span class="errormsg"> <?= $errors['usrname'] ?> <?= $errors['passwd'] ?> <?= $errors['email'] ?> <?= $errors['phone'] ?> </span> </dd> </dl> <dl> <dt><label for="usrname">Username:</label></dt> <dd><input id="usrname" tabindex="1" accesskey="u" type="text" name="login" size="20" value="<?= htmlentities($values['usrname']) ?>" /></dd> </dl> <dl> <dt><label for="passwd">Password:</label></dt> <dd><input id="passwd" tabindex="2" accesskey="p" type="text" name="passwd" size="20" value="<?= htmlentities($values['passwd']) ?>" /></dd> </dl> <dl> <dt><label for="confirmpasswd">Confirm Password:</label></dt> <dd><input id="confirmpasswd" tabindex="3" accesskey="c" type="text" name="confirmpasswd" size="20" value="<?= htmlentities($values['confirmpwd']) ?>" /></dd> </dl> <dl> <dt> </dt> </dl> <dl> <dt><label for="email">E-mail:</label></dt> <dd><input id="email" tabindex="4" accesskey="e" type="text" name="email" size="20" value="<?= htmlentities($values['email']) ?>" /></dd> </dl> <dl> <dt><label for="confirmemail">Confirm E-mail:</label></dt> <dd><input id="confirmemail" tabindex="5" accesskey="f" type="text" name="confirmemail" size="20" value="<?= htmlentities($values['confirmemail']) ?>" /></dd> </dl> <dl> <dt> </dt> </dl> <dl> <dt><label for="name">Name:</label></dt> <dd><input id="name" tabindex="6" accesskey="n" type="text" name="name" size="20" value="<?= htmlentities($values['name']) ?>" /></dd> </dl> <dl> <dt><label for="phone">Phone:</label></dt> <dd><input id="phone" tabindex="7" accesskey="p" type="text" name="phone" size="20" value="<?= htmlentities($values['phone']) ?>" /></dd> </dl> <dl> <dt><input tabindex="8" accesskey="r" type="reset" value="Reset" /></dt> <dd><input tabindex="9" accesskey="r" type="submit" value="Register" /></dd> </dl> </fieldset> </form> <div class="push"></div> </div> <div class="footer"> <ul> <li><?php $ip = $_SERVER['REMOTE_ADDR']; echo "your ip address: $ip"; ?></li> <li><?php $remotehostname = gethostbyaddr($_SERVER['REMOTE_ADDR']); echo "your connection name: $remotehostname"; //Host name http://www.webmasterworld.com/forum88/12269.htm ?></li> <li>server doc root: <?php echo $_SERVER["DOCUMENT_ROOT"]; ?></li> <li>© Dataflow Mechanics, 2009. Just kidding, there's no copyright here.</li> </ul> </div> </body> </html> <?php } function ProcessForm($values){ mail('[email protected]', 'Form test', $values['text'], "From: \"{$values['name']}\" <{$values['email']}>"); // Replace with actual page or redirect echo "<html><head><title>Thank you!</title></head><body>It's done!</body></html>"; } if ($_SERVER['REQUEST_METHOD'] == 'POST'){ $formValues = $_POST; $formErrors = array(); if (!VerifyForm($formValues, $formErrors)) DisplayForm($formValues, $formErrors); else ProcessForm($formValues); } else DisplayForm(null, null); ?> Link to comment https://forums.phpfreaks.com/topic/139071-issue-php-form-processing-verification-in-page/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.