mail form validation

Discussion in 'PHP' started by mnymkr, Oct 15, 2007.

  1. #1
    ok so somehow my php mail form just broke....and I don' t know why so I want to build it from scratch and learn from it

    I need to pass two fields from a form and send it to myself.

    I need to validate these two with javascript

    one field is email and the other is phone number

    if either of these is incorrect there needs to be an errror message

    i would like to send html and text version of the email also

    i keep trying but somehow it just isn't sending to my email address.

    thanks!
     
    mnymkr, Oct 15, 2007 IP
  2. undir

    undir Peon

    Messages:
    696
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can you show us the code?
     
    undir, Oct 15, 2007 IP
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3
    <?php
    
    
    
    
    
    
    
    
    
    
    /* All form fields are automatically passed to the PHP script through the array 
    $email = $_GET['email'];
    $comments = $_GET['comments'];
    $message = "doodie";
    
    
    /* PHP form validation: the script checks that the Email field contains a valid email address and the Subject field isn't empty. preg_match performs a regular expression match. It's a very powerful PHP function to validate form fields and other strings - see PHP manual for details. */
    if (!preg_match("/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", $email)) {
      echo "<h4>Invalid email address</h4>";
      echo "<a href='javascript:history.back(1);'>Back</a>";
    } elseif ($subject == "") {
      echo "<h4>No subject</h4>";
      echo "<a href='javascript:history.back(1);'>Back</a>";
    }
    
    /* Sends the mail and outputs the "Thank you" string if the mail is successfully sent, or the error string otherwise. */
    elseif (mail($email,$subject,$message)) {
      echo "<h4>Thank you for sending email</h4>";
    } else {
      echo "<h4>Can't send email to $email</h4>";
    }
    ?>
    Code (markup):

    this is what i was trying out.

    this was just to see if i could validate it not exactly what i want to do but i am trying to learn.
     
    mnymkr, Oct 15, 2007 IP