Jump to content

Contact form not working


pensyd

Recommended Posts

Hi Guys,

 

I am sure you will laugh at my coding attempt, but i need your help.

 

I have set up a contact form and a PHP process to send the email.

 

Everything appears to be working except the sending part.

 

All fields validate as they should, the form forward to a thank you page as it should.

But no emails are received either at my domain end or from the sender i am testing with.

 

Here is my form code:

<form id="form1" name="form1" method="post" action="emailform.php" onsubmit="window.setTimeout(function(){window.location='/thankyou.html'},20); return true;">
<p><span id="sprytextfield1">
<label for="name">Name: </label>
<input name="name" type="text" id="name" tabindex="10" />
<span class="textfieldRequiredMsg">Your name is required.</span><span class="textfieldMaxCharsMsg">No more than 50 characters in your Name.</span><span class="textfieldMinCharsMsg">Your Name must be atleast 4 characters long.</span></span></p>
<p><span id="sprytextfield2">
<label for="email">Email: </label>
<input type="text" name="email" id="email" tabindex="20" />
<span class="textfieldRequiredMsg">An Email Address is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span><span class="textfieldMaxCharsMsg">Exceeded maximum number of characters.</span></span></p>
<p><span id="spryselect1">
  <label for="state">State</label>
  <select name="state" id="state" tabindex="30">
    <option>ACT</option>
    <option>NSW</option>
    <option>Queensland</option>
    <option>Victoria</option>
    <option>Tas</option>
    <option>SA</option>
    <option>WA</option>
    <option>NT</option>
  </select>
  <span class="selectInvalidMsg">Please select a valid State.</span></span></p>
<p>What styles are you interested in doing? 
  <label for="styles"></label>
  <select name="styles" id="styles" tabindex="40">
    <option>Portraits</option>
    <option>Fashion</option>
    <option>Swimwear</option>
    <option>Lingerie</option>
    <option>Art Nude</option>
    <option>All Styles Possible</option>
    <option>Just Curious for Now</option>
  </select>
</p>
<p>Please leave any Message or Comments Below:</p>
<p><span id="sprytextarea1">
<label for="comments"></label>
<textarea name="comments" id="comments" cols="45" rows="5" tabindex="50"></textarea>
<span class="textareaRequiredMsg">Please enter your comments here.</span><span class="textareaMaxCharsMsg">Exceeded maximum number of characters.</span></span></p>
<p>
  <label for="submit"></label>
  <input type="submit" name="submit" id="submit" value="Send Form" tabindex="50" />
</p>
    </form>

 

Here is the PHP Code:

<?php

/* Subject and variables */

$emailSubject = 'New Email Enquiry';
$webMaster = 'enquiry@davids-photography.com';


/* Gathering data variables */

$nameField = $_POST['name'];
$emailField = $_POST['email'];
$stateField = $_POST['state'];
$stylesField = $_POST['styles'];
$commentsField = $_POST['comments'];

$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
State: $state <br>
Style: $style <br>
Comment: $comments <br>
EOD;

$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

?>

 

Any help you can offer would be greatly appreciated. I am not looking for anything over the top, just something simple that works.

 

Thanks

 

David

Link to comment
Share on other sites

Hey Pensyd, I think I know what why your script is not working.

 

I think the onSubmit function does not allow the form to actually post to the file you want.

 

Firstly change this

<form id="form1" name="form1" method="post" action="emailform.php" onsubmit="window.setTimeout(function(){window.location='/thankyou.html'},20); return true;">

 

To this

<form id="form1" name="form1" method="post" action="emailform.php">

 

The use the following PHP code instead of your own

<?php

/* Subject and variables */
$emailSubject = 'New Email Enquiry';
$webMaster = 'enquiry@davids-photography.com';

/* Gathering data variables */
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$stateField = $_POST['state'];
$stylesField = $_POST['styles'];
$commentsField = $_POST['comments'];

$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
State: $state <br>
Style: $style <br>
Comment: $comments <br>
EOD;

$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

if($sucess) {
header("Location: /thankyou.html");
} else {
echo 'Put a nice error message here or redirect back to the form.';
}

?>

 

Thanks, Paul.

Link to comment
Share on other sites

Hi Guys,

 

Paul, i made the changes you suggested and it came up with the error message.

 

So i put in the error report as suggested and got this reply,

 

Notice: Undefined variable: name in /home/davidsp3/public_html/emailform.php on line 18

 

Notice: Undefined variable: email in /home/davidsp3/public_html/emailform.php on line 19

 

Notice: Undefined variable: state in /home/davidsp3/public_html/emailform.php on line 20

 

Notice: Undefined variable: style in /home/davidsp3/public_html/emailform.php on line 21

 

Notice: Undefined variable: comments in /home/davidsp3/public_html/emailform.php on line 21

 

Notice: Undefined variable: email in /home/davidsp3/public_html/emailform.php on line 24

 

Notice: Undefined variable: sucess in /home/davidsp3/public_html/emailform.php on line 28

The Form did not process properly, please go back and try again

 

Looks like its not liking the variable names in the script.

 

Any thoughts?

Link to comment
Share on other sites

These are the actual line numbers

 

Name: $name <br>  Line 17

Email: $email <br>    Line 18

State: $state <br>  Line 19

Style: $style <br>    Line 20

Comment: $comments <br>  Line 21

 

$headers = "From: $email\r\n";  Line 24

$headers .= "Content-type: text/html\r\n";

$success = mail($webMaster, $emailSubject, $body, $headers);

 

if($sucess) {  Line 28

header("Location: /thankyou.html");

} else {

echo 'The Form did not process properly, please go back and try again';

 

Thanks

 

Link to comment
Share on other sites

Apparently there are some typos in your original code.  You retrieve the POST fields into variables, BUT then use different names for the variables in the form:

 

/* Gathering data variables */
$nameField = $_POST['name'];    // YOU CALLED THIS $name IN THE CODE BELOW
$emailField = $_POST['email'];    // YOU CALLED THIS $email IN THE CODE BELOW
$stateField = $_POST['state'];    // YOU CALLED THIS $state IN THE CODE BELOW
$stylesField = $_POST['styles'];    // YOU CALLED THIS $style IN THE CODE BELOW
$commentsField = $_POST['comments'];    // YOU CALLED THIS $comments IN THE CODE BELOW

$body = <<<EOD
<br><hr><br>
Name: $name <br>
Email: $email <br>
State: $state <br>
Style: $style <br>
Comment: $comments <br>
EOD;

 

There's also a missing "c" in the second $success reference.

 

Link to comment
Share on other sites

Thanks DavidAM and everyone else for you help.

 

It is working now.  I greatly appreciate your help.

 

I have just one more question sorry.  If i want to send and email to the person who filled out the form, with a small message in the subject line.

 

What should i put in the code?

 

Thanks

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.