Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Please help with email form.

    Hello, I am trying to use a PHP script to implement an email form on a page. I cant get it to work so any feedback will help. The HTML is as follows...

    Code:
    <form method="post" action="mailform.php">
    	<div>
    		<div class="row half">
    			<div class="6u">
    				<input type="text" name="name" id="name" placeholder="Name" />
    			</div>
    			<div class="6u">
    				<input type="text" name="email" id="email" placeholder="Email" />
    			</div>
    		</div>
    		<div class="row half">
    			<div class="12u">
    				<input type="text" name="subject" id="subject" placeholder="Subject" />
    			</div>
    		</div>
    		<div class="row half">
    			<div class="12u">
    				<textarea name="message" id="message" placeholder="Message"></textarea>
    			</div>
    		</div>
    		<div class="row half">
    			<div class="12u">
    				<input type="text" name="human" placeholder="*What is 2+2? (Anti-spam)">
    			</div>
    		</div>
    		<div class="row">
    			<div class="12u">
    				<a href="#" class="button form-button-submit">Send Message</a>
    				<a href="#" class="button button-alt form-button-reset">Clear Form</a>
    			</div>
    		</div>
    	</div>
    </form>
    The PHP script is as follows...

    PHP Code:
    <?php
        $name 
    $_POST['name'];
        
    $email $_POST['email'];
        
    $subject $_POST['subject'];
        
    $message $_POST['message'];
        
    $from 'From: Example.com'
        
    $to 'Example@gmail.com'
        
    $subject 'Example.com Contact Form';
        
    $human $_POST['human'];
                
        
    $body "From: $name\n E-Mail: $email\n Message:\n $message";
                    
        if (
    $_POST['submit'] && $human == '4') {                 
            if (
    mail ($to$subject$body$from)) { 
            echo 
    '<p>Your message has been sent!</p>';
        } else { 
            echo 
    '<p>Something went wrong, go back and try again!</p>'
        } 
        } else if (
    $_POST['submit'] && $human != '4') {
        echo 
    '<p>You answered the anti-spam question incorrectly!</p>';
        }
    ?>
    Is any of this correct? Am I fine with the contact form being in index.html while the PHP script is saved as a .PHP? Any info would be greatly appreciated.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,194
    Thanks
    80
    Thanked 4,563 Times in 4,527 Posts
    Mostly okay, but we need to see the JavaScript code that is used by your <a> tag to then submit the <form>.

    You won't have any $_POST['submit'] since you are using <a> to do the submittal. So either get rid of its usage or figure out an alternative.

    This line
    Code:
    if (mail ($to, $subject, $body, $from))
    should instead be
    Code:
    if (mail ($to, $subject, $body, $email))
    That way, if the user gives a bogus email address *MAYBE* the PHP mailer will reject the send.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •