?
Solved

challenge with php

Posted on 2011-10-05
5
Medium Priority
?
307 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:antatiana
5 Comments
 
LVL 5

Expert Comment

by:PragmatiCoder
ID: 36921705
Post the error that you are getting (if any) and the php version you are using
0
 
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36921825
And what you'd like the script to do and what it is doing instead.
0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 total points
ID: 36922006
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
 
LVL 5

Accepted Solution

by:
Neil_Bradley earned 1000 total points
ID: 36922015
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
 

Author Closing Comment

by:antatiana
ID: 36933503
Thank you for your help!
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question