Hello,
I have code below (I apologize for the length). I am validating an email address. It checks to see if the email field is blank, in the proper format, etc... It prints errors to the screen (at the very bottom of the code) and everything works perfectly, but the errors always print at the very top of the page and I am having difficulty moving those errors around so they fit nicely into the page design I have. Does anyone have any thoughts on this?
Thanks in advance!!!
<?php
// Load PEAR DB
require 'DB.php';
require '../../formhelpers.php';
require '../../../../dbConnect/DbConnect.php';
// Connect to the database
$db = DB::connect($connect);
if (DB::isError($db)) { die ("Can't connect: " . $db->getMessage()); }
// Set up automatic error handling
$db->setErrorHandling(PEAR_ERROR_DIE);
// Set up fetch mode: rows as objects
$db->setFetchMode(DB_FETCHMODE_ASSOC);
if ($_POST['_submit_check']) {
process_form();
}
?>
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title> Music Spotlight Newsletter Sign Up</title>
</head>
<body>
<div align="center">
<table border="0" width="860" cellspacing="0" cellpadding="0" height="585" id="table1" background="images/EmailCapture_Background.jpg">
<tr>
<td valign="top">
<div align="center">
<table border="0" width="363" cellspacing="0" cellpadding="0" id="table2">
<tr>
<td height="96"> </td>
</tr>
<tr>
<td align="center">
<font face="Arial" size="2">
Music's mission is to help bring back a sense of
discovery to music. So do
a ...and get back to Music.</font></td>
</tr>
<tr>
<td align="center">
<hr color="#FFFFFF" size="1"></td>
</tr>
<tr>
<td align="center">
<font face="Arial" size="2">Enter
your Name (optional) & Email below to receive updates & specials from
Music.</font></td>
</tr>
<tr>
<td align="center"> </td>
</tr>
<tr>
<td align="center"><font face="Arial" size="2" color="red"><b><?php echo $error;?></b></font></td>
</tr>
<tr>
<td align="center">
<form method="POST" action="<?php print $_SERVER['PHP_SELF']; ?>">
<div align="center">
<table border="0" width="100%" cellspacing="1" cellpadding="2" id="table3">
<tr>
<td width="141" align="right">
<font size="2" face="Arial">
</font><font size="1" face="Arial"><i><b>(optional)
</b></i> </font>
<font size="2" face="Arial">
</font><i><b><font size="2" face="Arial">* </font>
</b></i><font size="2" face="Arial">
First Name:</font></td>
<td>
<input type="text" name="firstName" size="40" style="font-size: 10px; font-family: Arial"></td>
</tr>
<tr>
<td width="141" align="right">
<i><b><font size="1" face="Arial">
(optional) </font></b></i>
<font size="2" face="Arial">
</font><i><b><font size="2" face="Arial">*</font></b></i><font size="2" face="Arial">
Last Name:</font></td>
<td>
<input type="text" name="lastName" size="40" style="font-size: 10px; font-family: Arial"></td>
</tr>
<tr>
<td width="141" align="right">
<font face="Arial" size="2">Email:</font></td>
<td>
<input type="text" name="email" size="40" style="font-size: 10px; font-family: Arial"></td>
</tr>
</table>
</div>
<p>
<input type="hidden" name="_submit_check" value="1"/>
<?php input_submit('save','Submit'); ?>
<input type="reset" value="Reset" name="B2"></p>
</form>
<p><font face="Arial" size="1"><font color="#666666">
Visit Us online at </font>
<a target="_blank" href="http://www..com">
<font color="#000000">.com</font></a></font></td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</div>
</body>
</html>
<?php
function process_form() {
global $db;
$firstName = addslashes($_POST['firstName']);
$lastName = addslashes($_POST['lastName']);
$email = $_POST['email'];
$emailHash = md5($email);
$active = "n";
// Check syntax
$validEmailExpr = "^[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*" .
"@[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*$";
// Validate the email
if (empty($email))
{
print "The email field cannot be blank";
return false;
}
elseif (!eregi($validEmailExpr, $email))
{
print "The email must be in the name@domain format.";
return false;
}
elseif (strlen($email) > 30)
{
print "The email address can be no longer than 30 characters.";
return false;
}
elseif (function_exists("getmxrr") && function_exists("gethostbyname"))
{
// Extract the domain of the email address
$maildomain = substr(strstr($email, '@'), 1);
if (!(getmxrr($maildomain, $temp) ||
gethostbyname($maildomain) != $maildomain))
{
print "The domain does not exist.";
return false;
}
}
$db->query('INSERT INTO users (firstName, lastName, email, hash, active)
VALUES (?,?,?,?,?)',
array($firstName, $lastName, $email, $emailHash, $active));
$message = "
Thank you for signing up for the . Please click on the link below to confirm your registration. If the link is not active, copy-and-paste it into your browser window.
http://www.EmailCapture/validateSuccess.php?id=$emailHash
";
$subject = "";
$from = "";
mail("$email", "$subject", "$message", "From: $from");
print "<script>window.location='ThankYou.htm'</script>";
}
?>