PHP Question

Discussion in 'Web Design and Development' started by John444, Aug 17, 2011.

  1. John444 macrumors member

    Joined:
    Feb 10, 2011
    #1
    I created a contact form in html to link with a send.php file. I have no knowledge of php and everywhere I look on the internet I cannot find what I want. I can get it to send the email but it wont show any of the fields.

    This is how I want the email to look:

    Name: whatever they typed

    Email: email@email.com

    Number: 123-4567

    Message: bla bla bla

    Can someone please give me the php for this? (the names of the fields in the form are the same as above Name, Email, Number, Message).
     
  2. frocco macrumors 6502

    frocco

    Joined:
    Jan 27, 2009
    #2
    In your php file,
    use $_POST['Email'] to get the value.
     
  3. John444 thread starter macrumors member

    Joined:
    Feb 10, 2011
    #3
    Thanks for the reply!

    So I use $POST['Name'] for all the values, I know that part but how do I get it to email to me. I use the mail[$to,$subject,$content] but that only gives me the subject and the message. But I want it to display the name, email, phone number and the message in the email that is sent to my email address.

    any ideas?
     
  4. skylerr macrumors newbie

    Joined:
    Aug 9, 2011
    #4
    Try this

    <?php
    $to = "your@email.com";
    $from = "noreply@email.com";
    $subject = "Your subject";
    $name = $_POST['Name'];
    $email = $_POST['Email'];
    $number = $_POST['Number'];
    $message = $_POST['Message'];
    $body=
    "
    Name: $name
    <br /><br />
    Email: $email
    <br /><br />
    Number: $number
    <br /><br />
    Message: $message
    <br /><br />
    ";
    $headers = "From: $from \r\n";
    $headers .= "Content-Type: text/html; charset=ISO-8859-1 \r\n";
    $headers .= "MIME-Version: 1.0 \r\n";
    mail($to, $subject, $body, $headers);
    ?>
     
  5. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #5
    And if you're putting this form out in the open, prepare to be bombarded with spam messages. Also, using $_POST data without any server-side validation is a very bad idea.

    Since you don't know PHP, you might be better served using one of the millions of available form-to-email PHP scripts that you can find with a quick Google search. Just about any of them will be better than what was posted above.

    To get you started...
     
  6. John444 thread starter macrumors member

    Joined:
    Feb 10, 2011
    #6
    I know javascript really well, I already made a javascript validation, but I am going to do research a lot to make a php validation incase they have javascript disabled. thanks

    ----------

    Thanks, it works great, know I just have to create a php validation
     
  7. turbobass macrumors 6502

    Joined:
    May 25, 2010
    Location:
    Los Angeles
    #7
    Just out of curiousity, how do you validate that something like this is coming from within the server?
     
  8. John444, Aug 20, 2011
    Last edited: Aug 20, 2011

    John444 thread starter macrumors member

    Joined:
    Feb 10, 2011
    #8
    there is a link up above that leads to a google search, there are tons of ways to validate info using a server side script or javascript. For example
    You could do something like this:

    <?php
    $to = "Email@email.com";
    $from = " Your Website";

    $test="2";

    if($_POST['Name']==""){
    $test="1";
    }
    if($_POST['Email']==""){
    $test="1";
    }
    if($_POST['Number']==""){
    $test="1";
    }
    if($_POST['Message']==""){
    $test="1";
    }

    if($test=="1")
    {
    echo "<html><head>
    <meta http-equiv=\"refresh\" content=\"10;url=contact.html\"/>
    </head>
    <body bgcolor=\"black\">
    <font color=\"red\"><h1>There was an Error, Please ensure you filled in all the fields!</h1></font>
    </body></html>
    ";
    }
    else{

    $subject = "Website Submission";
    $name = $_POST['Name'];
    $email = $_POST['Email'];
    $number = $_POST['Number'];
    $message = $_POST['Message'];
    $body=
    "
    Name: $name\n
    Email: $email\n
    Number: $number\n
    Message: $message

    ";

    $headers = "From: $from \n";





    mail($to, $subject, $body, $headers);


    echo "<html>
    <head>
    <meta http-equiv=\"refresh\" content=\"5;url=index.html\"/>
    </head>


    <body bgcolor=\"black\">

    <font color=\"white\">Thank You! Your form was sent successfully</font>

    </body>


    </html>
    ";


    }
    ?>

    But this is just simple verification in PHP, I don't have much knowledge of PHP so this is the best I can do. With javascript you have a ton of ways you can verify it in great detail, and have it validate instantly on the same page they filled out the form. So I would only use this verification as a backup incase they have javascript disabled.
     
  9. Ronald Williams macrumors member

    Joined:
    Jul 11, 2011
    #9
    follow these steps to create a web form to email you:

    1: Create a html or php file and code the html form into it.
    2: Use javascript functions to validate all the inputs before submitting the form.
    3: In the php file use a suitable mail function(download code from the internet to get saved from the trouble) to mail the contents to yourself.

    Also don't forget to configure your mail server when doing that.
    Either do all of the above or....download some php mail sample to make things going.
     
  10. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #10
    For validating contact forms, email injection is one of the biggest issues you want to validate on. Your form can be turned into a spam propagation tool very easily. I highly suggest you read the linked page so you can do some minimal validation. The validation you provided here doesn't do much of anything to block spam.
     
  11. John444 thread starter macrumors member

    Joined:
    Feb 10, 2011

Share This Page