0

I'm trying to allow people who visit my website to subscribe by filling out a short form and having the website send me an email with the information, but at the moment the form won't even submit the information. Some help would be greatly appreciated.

<form id="ContactForm" action="mail/MailHandler.php">
  <div class="success">Form submitted!<br><strong>Look forward to our next Monthly Mailer.</strong></div>
  <fieldset>
    <div class="wrapper">
      <label class="name">
        <span class="bg"><input type="text" value="Name:" class="input"></span>
        <span class="error">*This is not a valid name.</span> 
        <span class="empty">*This field is required.</span> 
      </label>
    </div>
    <div class="wrapper">
      <label class="email">
        <span class="bg"><input type="text" value="E-mail:" class="input"></span>
        <span class="error">*This is not a valid email address.</span> 
        <span class="empty">*This field is required.</span>
      </label>
    </div>
    <div class="btns">
      <a href="mail/MailHandler.php" class="button1" data-type="submit">
        <span><strong>submit</strong></span>
        <span class="active"><strong>submit</strong></span>
      </a>
    </div>
  </fieldset>
</form>

with the PHP as follows

<?php
$owner_email = $_POST["myemail@address.com"];
$headers = 'From:' . $_POST["email"];
$subject = 'Monthly Mailer Subscriber ' . $_POST["name"];
$messageBody = "";

$messageBody .= '<p>' . $_POST["name"] . ' would like to be subscribed to your Monthly Mailer!</p>' . "\n";
$messageBody .= '<br>' . "\n";
$messageBody .= '<p>Email Address: ' . $_POST['email'] . '</p>' . "\n";
$messageBody .= '<br>' . "\n";

try{
    if(!mail($owner_email, $subject, $messageBody, $headers)){
        throw new Exception('mail failed');
    }else{
        echo 'mail sent';
    }
}catch(Exception $e){
    echo $e->getMessage() ."\n";
}
?>

PROBLEM SOLVED! The problem lied in that my inputs did not have names, and the syntax of $owner_email = $_POST["myemail@address.com"]; should have been $owner_email = 'myemail@address.com';

2 Answers2

4

Problem #1

You're looking for POST variables but are sending them via GET since you never explicitly set it to POST. To solve this change:

<form id="ContactForm" action="mail/MailHandler.php">

to:

<form id="ContactForm" action="mail/MailHandler.php" method="POST">

Problem #2

You forgot to give your inputs names:

<input type="text" value="Name:" class="input">

should be:

<input type="text" value="Name:" name="name" class="input">

Problem #3

You lack a submit button:

<input type="submit" value="submit">

That should replace:

<a href="mail/MailHandler.php" class="button1" data-type="submit">
    <span><strong>submit</strong></span>
    <span class="active"><strong>submit</strong></span>
  </a>
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • I tried this but no dice. I'm not entirely sure how php works, but do I need some kind of js to go with these? – user3224788 Jan 22 '14 at 19:39
  • No javascript should be required for this to work. Does *anything* happen when the submit button is pressed? If not your HTML is invalid somehow. – John Conde Jan 22 '14 at 19:41
  • Oh! I got it to do _something_... I got it to throw the "mail failed" exception... Well at least I know it's submitting something now. – user3224788 Jan 22 '14 at 20:03
  • Here's the website just in case. The form is at the bottom of the page. http://cisnerosdesignstudio.com/#!/page_monthly_mailers – user3224788 Jan 22 '14 at 20:11
  • PROBLEM SOLVED! The problem lied in that my inputs did not have names, and the syntax of $owner_email = $_POST["myemail@address.com"]; should have been $owner_email = 'myemail@address.com'; – user3224788 Jan 22 '14 at 21:53
0

You're not setting your form element names.

You need to change your inputs to the following:

    <input type="text" value="E-mail:" name="email" class="input">
    <input type="text" value="Name:" name="name" class="input">