Jump to content

[SOLVED] unsure syntax


NickG21

Recommended Posts

hey everyone, below is my code for the syntax and server validation of an e-mail address.  it is a simple form with one text box for the info and a submit button, however the code isn't executing correctly and when i initially open the page there is the invalid e-mail error displayed on the top.  could anyone tell me what exactly is wrong with my syntax here?
thank you

[code]<?php
function checkEmail($email)
{
  if (!preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+([a-zA-Z0-9\._-] +)+$/" , $email)) {
return false;
}
list($username,$domain) = split('@',$email);
$mxhosts = array();
if(!getmcxrr($domain, $mxhosts)) {

  if(!fsockopen($domain, 25, $errno, $errstr, 30))
  {
      return false;
  }else{
 
foreach($mxhosts as $host) {
if (fsockopen($host,25,$errno,$errstr,30)) {
return true;
}
}
return false;
}
}
if(!fsockopen($domain,25,$errno,$errstr,30)) {
return false;
}else{
return true;
}
}


$email = trim($_POST['email']); 
    if(!checkEmail($email)) {
    echo 'Invalid email address!';
    }
    else {
    echo 'Email address is valid';
    }
if (isset($_POST['submit'])) {
$email = $_POST['email'];
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="emailcheck.php">
<table class="qForm">
<tr>
<td><a>Email:</a></td>
<td><input type="text" size="28" name=email value-<? echo"$email"?>"><br/></td>
</tr>
<tr>
<input type="hidden" value="1"
<td><input type="submit" name="submit" value="submit"></td>
</tr>
</table>
</form>
</body>
</html>[/code]
Link to comment
https://forums.phpfreaks.com/topic/31977-solved-unsure-syntax/
Share on other sites

You should only do the email check if the form has been submitted.

Change this:
[code]<?php
$email = trim($_POST['email']); 
    if(!checkEmail($email)) {
    echo 'Invalid email address!';
    }
    else {
    echo 'Email address is valid';
    }
if (isset($_POST['submit'])) {
$email = $_POST['email'];
}?>[/code]

to
[code]<?php
if (isset($_POST['submit'])) {
    $email = trim($_POST['email']); 
    if(!checkEmail($email))
          echo 'Invalid email address!';
    else
    echo 'Email address is valid';
}?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/31977-solved-unsure-syntax/#findComment-148391
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.