Contact form ?

Discussion in 'PHP' started by Xabber, Sep 11, 2007.

  1. #1
    Can someone please give me detailed tutorial / guide on how to make a contact form ? I know XHTML and CSS, and just a very little bit PHP :p

    Thanks in advance.
     
    Xabber, Sep 11, 2007 IP
  2. Jasonb

    Jasonb Well-Known Member

    Messages:
    4,486
    Likes Received:
    151
    Best Answers:
    0
    Trophy Points:
    195
    #2
    all you need is a little file called sendmail.php and a form which you can create using dream weaver
     
    Jasonb, Sep 11, 2007 IP
  3. Xabber

    Xabber Active Member

    Messages:
    437
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #3
    I made the form, I just need a PHP file. I can read PHP, so I can edit it out :)
     
    Xabber, Sep 11, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
  5. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #5
    suppose your form is passing the following
    name, email, phone, message .. and let's suppose it's a POST method

    so here's your script

    $name = $_post['name'];
    $email = $_post['email'];
    $phone = $_post['phone'];
    $message = $_post['message'];
    
    if ($name && $email && $phone && $message) {   // check the required fields
    
    $body = "Yo Xabber, wassup ? <br><br>\n $name has just fill out the contact form.<br>\n You may contact $name by phone at $phone or by email at $email .<br>\n";
    $body .= "Here is what $name wants from you:<br><br>\n";
    $body .= $message;
    
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    $headers .= 'From: $name <$email>' . "\r\n";
    
    mail("xabber@xabber.com", "YourSite.com - Contact Form", $body, $headers);
    }
    else {
    echo "Yo, you gotta fill out the required fields .. go back and fill it out";
    }
    
    
    PHP:
    sorry for too much kidding :)

    is this good for you ? :) - then gimme a rep :)

    Hope this helps
     
    stats, Sep 11, 2007 IP
  6. ukfreelancer

    ukfreelancer Peon

    Messages:
    145
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ukfreelancer, Sep 11, 2007 IP
  7. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #7
    here's some more contribution for you

    function isValidAddress( $email, $check = true )
    {
      if (!ereg(''
          . '^'
          . '[-!#$%&\'*+/0-9=?A-Z^_a-z{|}~]'
          . '(\\.?[-!#$%&\'*+/0-9=?A-Z^_a-z{|}~])*'
          . '@'
          . '[a-zA-Z](-?[a-zA-Z0-9])*'
          . '(\\.[a-zA-Z](-?[a-zA-Z0-9])*)+'
          . '$'
          , $email
          )    ) return false;
      list( $local, $domain ) = split( "@", $email, 2 );
      if ( strlen($local) > 64 || strlen($domain) > 255 ) return false;
      if ( $check && !gethostbynamel( $domain ) ) return false;
      return true;
    }
    
    function isValidPhone($phone) {
    $phone = preg_replace("^\D^", "", $phone);
    $phone = preg_replace("^\s^", "", $phone);
    return (strlen($phone) >= 10 ?  true : false);
    }
    
    PHP:
    just copy and paste these functions into your php, and after that in your script where it says

    if ($email && $phone && $message && $name)

    just replace that line with the following

    if (isValidAddress($email, true) && isValidPhone($phone) && $name && $email)

    so now the script will check for valid email and valid US 10 digit phone with area code

    NOTE:
    1. the isValidAddress function checks also for validity of the domain (actually checks if the domain exists at all) of the email if the second argument is set to "true" and doesn't check it if set to "false"
    2. the isValidAddress function is not written by me
     
    stats, Sep 11, 2007 IP
  8. Lapzwans

    Lapzwans Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I created a contact form, and a PHP-file "sendmail.php". But when I click the submit button, the computer asks me whether I want to run the PHP-file, or save it to disk, like you get when you download a file.
    What's wrong ?
     
    Lapzwans, Sep 17, 2007 IP
  9. Aron Schatz

    Aron Schatz Peon

    Messages:
    201
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Are you using apache? Does apache know to parse PHP files using the PHP command?
     
    Aron Schatz, Sep 17, 2007 IP
  10. Xabber

    Xabber Active Member

    Messages:
    437
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #10
    Tried the tutorials submitted, worked like a charm. Thanks !!
     
    Xabber, Sep 17, 2007 IP
  11. Lapzwans

    Lapzwans Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hi Aron,
    Thanks for replying.
    I'm not using Apache. I just get the message no matter what the content of the PHP-file is. Like when I copy and paste code on this site, for instance.
     
    Lapzwans, Sep 17, 2007 IP
  12. Jem

    Jem Peon

    Messages:
    22
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    "stats" code is susceptible to header injection because of the unvalidated $name variable in the email headers. You should remove $name or validate it first. You can find a mail form that protects against all this via the link in my signature.

    Lapzwans: you can't run PHP on your computer unless you have the correct software installed. If you're planning on doing a lot of editing of PHP files on your computer I suggest a package called "XAMPP" (google it) - it contains all you'll need to execute PHP files and more.
     
    Jem, Sep 18, 2007 IP