challenge with php

I can't understand what's wrong with this php script.
?php print('<?xml version ="1.0" encoding ="utf-8"?>' ) ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>


<?php
   // This function tests whether the email address is valid  
   function isValidEmail($email){
      $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
     
      if (eregi($pattern, $email)){
         return true;
      }
      else {
         return false;
      }   
   }
?>   
<head>
  <title>Email validation form</title>
</head>
<body>
  <form action="EmailValidation.php" method="post" name="EmailValidation">
     <table>
       <tr><td>Email:<input name="email"></td><tr>
       <?php
         if (isset($_POST['submitemail']))
         {
            if (isValidEmail($_POST['email'])){
                echo "<tr><td>The email: ".$_POST['email']." is valid!</td></tr>";
            }
            else{
                echo "<tr><td>The email: ".$_POST['email']." is invalid!</td></tr>";
            }
         }
       ?>
       <tr><td><input type="submit" name="submitemail" value="Validate email"></td></tr>
     </table>
  </form>
</body>

Open in new window

antatianaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PragmatiCoderCommented:
Post the error that you are getting (if any) and the php version you are using
0
Hugh McCurdyCommented:
And what you'd like the script to do and what it is doing instead.
0
Dave BaldwinFixer of ProblemsCommented:
Seems to work ok once you fix the formatting errors.  The opening PHP tag was missing the "<" and there were two <head> sections.

'eregi' is deprecated so I put 'preg_match' in there and made the modifications to the matching string.  I left the old code in the comments so you could compare them.  http://us.php.net/manual/en/function.preg-match.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<?php
   // This function tests whether the email address is valid  
   function isValidEmail($email){
      //$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
      $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/";
     
      //if (eregi($pattern, $email)){
      if (preg_match($pattern, $email)){
         return true;
      }
      else {
         return false;
      }   
   }
?>   
<head>
  <title>Email validation form</title>
</head>
<body>
  <form action="EmailValidation.php" method="post" name="EmailValidation">
     <table>
       <tr><td>Email:<input name="email"></td><tr>
       <?php
         if (isset($_POST['submitemail']))
         {
            if (isValidEmail($_POST['email'])){
                echo "<tr><td>The email: ".$_POST['email']." is valid!</td></tr>";
            }
            else{
                echo "<tr><td>The email: ".$_POST['email']." is invalid!</td></tr>";
            }
         }
       ?>
       <tr><td><input type="submit" name="submitemail" value="Validate email"></td></tr>
     </table>
  </form>
</body>
</html>

Open in new window

0
Neil_BradleyWeb UX/UI DeveloperCommented:
See code. Works fine now. I have set the action of the form to POST to itself (as opposed to another page). This gives your function the ability to test the email address and output the result.
N

<?php print('<?xml version ="1.0" encoding ="utf-8"?>' ) ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>


<?php
   // This function tests whether the email address is valid  
   function isValidEmail($email){
      $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
     
      if (eregi($pattern, $email)){
         return true;
      }
      else {
         return false;
      }   
   }
?>   
<head>
  <title>Email validation form</title>
</head>
<body>
  <form action="<?=$_server['php_self']?>" method="post" name="EmailValidation">
     <table>
       <tr><td>Email:<input name="email"></td><tr>
       <?php
         if (isset($_POST['submitemail']))
         {
            if (isValidEmail($_POST['email'])){
                echo "<tr><td>The email: ".$_POST['email']." is valid!</td></tr>";
            }
            else{
                echo "<tr><td>The email: ".$_POST['email']." is invalid!</td></tr>";
            }
         }
       ?>
       <tr><td><input type="submit" name="submitemail" value="Validate email"></td></tr>
     </table>
  </form>
</body>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
antatianaAuthor Commented:
Thank you for your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.