1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[PHP Help] Simple E-mail Form

Discussion in 'General Programming Chat' started by ouchthathurts, Nov 10, 2011.

  1. ouchthathurts

    ouchthathurts Regular Member

    Joined:
    Feb 16, 2011
    Messages:
    438
    Likes Received:
    654
    Occupation:
    SEO
    Location:
    Japan
    Hey guys,

    Was wondering if someone could help me turn this html code into a working php script that emails me the details, I dont need to worry about captcha because I have a content lock box that pops up after they click submit.

    Code:
    <div class="sign-up-form">
                    <p>Sign up for this awesome service, <em>it'll only take a second!</em></p>
                        <ul>
                            <li><label>First Name</label>
                            <input type="text" /></li>
                            <li><label>Last Name</label>
                            <input type="text" /></li>
                            <li><label>Email Address</label>
                            <input type="text" /></li>
                            <li><label>Website</label>
                            <input type="text" /></li>
                        </ul>

    Any help would be greatly appreciated as my coding skills are minimum

    *Edit* Wow wrong section no idea how that happened - Mods could you move this over to PHP, Sorry!
     
  2. Xp3r7

    Xp3r7 Regular Member

    Joined:
    Sep 9, 2011
    Messages:
    286
    Likes Received:
    386
    Occupation:
    Freelancer
    Location:
    In the middle of nowhere
    Code:
    <?php
    if(isset($_POST["submit"])) {
    $firstName = htmlspecialchars(trim($_POST["firstName"]));
    $lastName = htmlspecialchars(trim($_POST["lastName"]));
    $email = htmlspecialchars(trim($_POST["email"]));
    $website = htmlspecialchars(trim($_POST["website"]));
    
    $message = "First name: ".$firstName."\n Last Name: ".$lastName."\n Email: ".$email."\n Website: ".$website;
    
    mail("yourEmail", "Some Subject", $message);  
    }
    
    ?>
    <div class="sign-up-form">
        <form action="" method="POST">
                    <p>Sign up for this awesome service, <em>it'll only take a second!</em></p>
                        <ul>
                            <li><label>First Name</label>
                            <input type="text" name="firstName" /></li>
                            <li><label>Last Name</label>
                            <input type="text" name="lastName" /></li>
                            <li><label>Email Address</label>
                            <input type="text" name="email" /></li>
                            <li><label>Website</label>
                            <input type="text" name="website" /></li>
                            <input type="submit" name="submit" value="Submit" />
                        </ul>
        </form>
    Hope this'll help you. =)
     
    • Thanks Thanks x 1
  3. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    Was about to paste pretty much the EXACT same piece of code:p
    Well... Too late-.-
     
    • Thanks Thanks x 1
  4. ouchthathurts

    ouchthathurts Regular Member

    Joined:
    Feb 16, 2011
    Messages:
    438
    Likes Received:
    654
    Occupation:
    SEO
    Location:
    Japan
    Wow thanks for the code !

    I just tested it and it looks great fits in perfectly with my theme but no email was sent!

    any ideas? I replaced mail("yourEmail", "Some Subject", $message); with my email address
     
  5. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    did you replace the whole line?
    like:
    mail("you@email.com");
    or just the part you are supposed to like
    mail("your@email.com", "Some Subject", $message);

    ?
    else, maybe tell us if there was any error code.
    or try this, and tell me if "Email Sent" shows up on your screen:

    Code:
    <?php
    if(isset($_POST["submit"])) {
    $firstName = htmlspecialchars(trim($_POST["firstName"]));
    $lastName = htmlspecialchars(trim($_POST["lastName"]));
    $email = htmlspecialchars(trim($_POST["email"]));
    $website = htmlspecialchars(trim($_POST["website"]));
    
    $message = "First name: ".$firstName."\n Last Name: ".$lastName."\n Email: ".$email."\n Website: ".$website;
    
    mail("yourEmail", "Some Subject", $message);  
    echo "email sent";
    }
    
    ?>
    <div class="sign-up-form">
        <form action="" method="POST">
                    <p>Sign up for this awesome service, <em>it'll only take a second!</em></p>
                        <ul>
                            <li><label>First Name</label>
                            <input type="text" name="firstName" /></li>
                            <li><label>Last Name</label>
                            <input type="text" name="lastName" /></li>
                            <li><label>Email Address</label>
                            <input type="text" name="email" /></li>
                            <li><label>Website</label>
                            <input type="text" name="website" /></li>
                            <input type="submit" name="submit" value="Submit" />
                        </ul>
        </form>
     
    • Thanks Thanks x 1
  6. StOn3D

    StOn3D Newbie

    Joined:
    Jun 6, 2011
    Messages:
    21
    Likes Received:
    14
    Home Page:
    Maybe your webhost has disabled the mail(); function?
     
    • Thanks Thanks x 1
  7. ouchthathurts

    ouchthathurts Regular Member

    Joined:
    Feb 16, 2011
    Messages:
    438
    Likes Received:
    654
    Occupation:
    SEO
    Location:
    Japan
    Thanks for the reply.

    I changed the line to
    mail("your@email.com", "Some Subject", $message);

    The form submits but just reloads like I have hit F5, there is no error message

    *random edit* 100th post :D
     
  8. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    if thats the case, maybe you should try writing the info to a file instead...
    Code:
    <?php
    function append($filename, $data) {
    		$fh = fopen($filename, 'a');
    		fwrite($fh, $data);
    		fclose($fh);
    	}
    if(isset($_POST["submit"])) {
    $firstName = htmlspecialchars(trim($_POST["firstName"]));
    $lastName = htmlspecialchars(trim($_POST["lastName"]));
    $email = htmlspecialchars(trim($_POST["email"]));
    $website = htmlspecialchars(trim($_POST["website"]));
    
    $message = "First name: ".$firstName."\n Last Name: ".$lastName."\n Email: ".$email."\n Website: ".$website;
    
    append("file.txt",$message."\n");
    echo "Data was written";
    }
    
    ?>
    <div class="sign-up-form">
        <form action="" method="POST">
                    <p>Sign up for this awesome service, <em>it'll only take a second!</em></p>
                        <ul>
                            <li><label>First Name</label>
                            <input type="text" name="firstName" /></li>
                            <li><label>Last Name</label>
                            <input type="text" name="lastName" /></li>
                            <li><label>Email Address</label>
                            <input type="text" name="email" /></li>
                            <li><label>Website</label>
                            <input type="text" name="website" /></li>
                            <input type="submit" name="submit" value="Submit" />
                        </ul>
        </form>
    
     
    • Thanks Thanks x 1
  9. Xp3r7

    Xp3r7 Regular Member

    Joined:
    Sep 9, 2011
    Messages:
    286
    Likes Received:
    386
    Occupation:
    Freelancer
    Location:
    In the middle of nowhere
    Grrr, my false in this post.

    Just use wkrappen91's way if the mail() doesn't work. But you need to be careful with the file naming, cause if someone find it, there'll be a new thread with title "Sell email list - good price!". =)
     
    • Thanks Thanks x 1
    Last edited: Nov 10, 2011
  10. ouchthathurts

    ouchthathurts Regular Member

    Joined:
    Feb 16, 2011
    Messages:
    438
    Likes Received:
    654
    Occupation:
    SEO
    Location:
    Japan
    Still no luck, doesnt seem to want to do anything just clears the data when i hit submit no file saved no email sent

    *EDIT*

    It saved and it sends!!!! I'm in idiot I saved the file as .html instead of .php!!


    Once again the users at blackhatworld save the day!!
     
    Last edited: Nov 10, 2011
  11. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    Sorry i have to say this:
    idiot:D


    Happy it's working now
     
    • Thanks Thanks x 1