Jump to content

Code error


pauldreed

Recommended Posts

I have a 'contact me' page with a form for visitors to enter details. The underlying code works great (thanks to earlier help in this forum), but to improve it, I want to add a new bit of code to email a confirmation email back to the visitor as well. I have attached a chunk of code below, but when I add the code between the 'send confirmation email' comments, the form errors. Can anyone see what the problem is please. I am very new to php and finding it difficult to remedy errors.
I have defined $sendnotification and $websitetitle earlier in the code.
[code]if(!($error = error()))
{
$sender_name = ucwords($_POST['name']);
$sender_email = $_POST['email'];
$headers = "From: $sender_name <$sender_email>\r\n";
$headers .= "Reply-To: $sender_name <$sender_email>\r\n";
$headers .= "Return-Path: $sender_name <$sender_email>\r\n";

$subject = 'Webform enquiry from '.$_POST['name'].' via '.$_SERVER['HTTP_HOST'];
$message =
"Sender: $sender_name \r\n\r\n".
"Email: $sender_email \r\n\r\n".
"Telephone: {$_POST['tel']} \r\n\r\n".
"Prefered Contact time: {$_POST['time']} \r\n\r\n".
"Message: {$_POST['comments']} \r\n\r\n".
$users_ip = 'IP Address of sender: '.$_SERVER['REMOTE_ADDR'];

if(mail($recipient, $subject, $message, $headers))

# send confirmation email
if($sendnotification == true) {
$notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly";
$notification_subject = "Thanks for your message to $websitetitle.";
mail($sender_email, $notification_subject, $notification_message, "From: $recipient");
}
# end of confirmation email code[/code]
Link to comment
Share on other sites

You are missing some closing brackets, try:
[code]

<?php

if(!($error = error()))
{
$sender_name = ucwords($_POST['name']);
$sender_email = $_POST['email'];
$headers = "From: $sender_name <$sender_email>\r\n";
$headers .= "Reply-To: $sender_name <$sender_email>\r\n";
$headers .= "Return-Path: $sender_name <$sender_email>\r\n";

$subject = 'Webform enquiry from '.$_POST['name'].' via '.$_SERVER['HTTP_HOST'];
$message =
"Sender: $sender_name \r\n\r\n".
"Email: $sender_email \r\n\r\n".
"Telephone: {$_POST['tel']} \r\n\r\n".
"Prefered Contact time: {$_POST['time']} \r\n\r\n".
"Message: {$_POST['comments']} \r\n\r\n".
$users_ip = 'IP Address of sender: '.$_SERVER['REMOTE_ADDR'];

if(mail($recipient, $subject, $message, $headers))
{
# send confirmation email
if($sendnotification == true) {
$notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly";
$notification_subject = "Thanks for your message to $websitetitle.";
mail($sender_email, $notification_subject, $notification_message, "From: $recipient");
}
# end of confirmation email code
}
}

?>

[/code]
Link to comment
Share on other sites

Thanks Alpine for your patience, but I'm afraid that doesnt work either.
I have included a slightly bigger chunk of code below, from the start up to the next section - function error() which may may make it clearer.
[code]<?php

# fill this in with the address to which the email will be sent
$recipient = 'paul@gmail.com';
# Send notification to sender (use false if not required)
$sendnotification = true;
# Your web site title (John Doe's Site)
$websitetitle = 'mysite';

session_start();
define('CAPTCHA_PATH', $_SERVER['DOCUMENT_ROOT'].'/captcha/');

if(isset($_GET['i']))
{
    captcha_image();
}
elseif(isset($_POST['submit']))
{
if(!($error = error()))
{
$sender_name = ucwords($_POST['name']);
$sender_email = $_POST['email'];
$headers = "From: $sender_name <$sender_email>\r\n";
$headers .= "Reply-To: $sender_name <$sender_email>\r\n";
$headers .= "Return-Path: $sender_name <$sender_email>\r\n";

$subject = 'Webform enquiry from '.$_POST['name'].' via '.$_SERVER['HTTP_HOST'];
$message =
"Sender: $sender_name \r\n\r\n".
"Email: $sender_email \r\n\r\n".
"Telephone: {$_POST['tel']} \r\n\r\n".
"Prefered Contact time: {$_POST['time']} \r\n\r\n".
"Message: {$_POST['comments']} \r\n\r\n".
$users_ip = 'IP Address of sender: '.$_SERVER['REMOTE_ADDR'];

if(mail($recipient, $subject, $message, $headers))

# send confirmation email
if($sendnotification == true) {
$notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly";
$notification_subject = "Thanks for your message to $websitetitle.";
mail($sender_email, $notification_subject, $notification_message, "From: $recipient");


        {
            header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success');
        }
        else
        {
            header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?failure');
        }
    }
}
[/code]
Now if I comment out this section;
[code]<?php
# send confirmation email
#if($sendnotification == true) {
#$notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly";
#$notification_subject = "Thanks for your message to $websitetitle.";
#mail($sender_email, $notification_subject, $notification_message, "From: $recipient");
    #}[/code]
the form is error free.
Link to comment
Share on other sites

Ok, there u have one bracket too much.

[color=orange]# send confirmation email
#if($sendnotification == true) {
#$notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly";
#$notification_subject = "Thanks for your message to $websitetitle.";
#mail($sender_email, $notification_subject, $notification_message, "From: $recipient");
    #[/color]}

Try:
[code]

<?php

# fill this in with the address to which the email will be sent
$recipient = 'paul@gmail.com';
# Send notification to sender (use false if not required)
$sendnotification = true;
# Your web site title (John Doe's Site)
$websitetitle = 'mysite';

session_start();
define('CAPTCHA_PATH', $_SERVER['DOCUMENT_ROOT'].'/captcha/');

if(isset($_GET['i']))
{
    captcha_image();
}
elseif(isset($_POST['submit']))
{
if(!($error = error()))
{
$sender_name = ucwords($_POST['name']);
$sender_email = $_POST['email'];
$headers = "From: $sender_name <$sender_email>\r\n";
$headers .= "Reply-To: $sender_name <$sender_email>\r\n";
$headers .= "Return-Path: $sender_name <$sender_email>\r\n";

$subject = 'Webform enquiry from '.$_POST['name'].' via '.$_SERVER['HTTP_HOST'];
$message =
"Sender: $sender_name \r\n\r\n".
"Email: $sender_email \r\n\r\n".
"Telephone: {$_POST['tel']} \r\n\r\n".
"Prefered Contact time: {$_POST['time']} \r\n\r\n".
"Message: {$_POST['comments']} \r\n\r\n".
$users_ip = 'IP Address of sender: '.$_SERVER['REMOTE_ADDR'];

if(mail($recipient, $subject, $message, $headers))

# send confirmation email
if($sendnotification == true) {
$notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly";
$notification_subject = "Thanks for your message to $websitetitle.";
mail($sender_email, $notification_subject, $notification_message, "From: $recipient");
            header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success');
        }
        else
        {
            header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?failure');
        }
    }
}

?>

[/code]
Link to comment
Share on other sites

The code you last pasted is still malformed.

The opening bracket ( [b]{[/b] ) before this line:

[code]header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success');[/code]

should be moved up to be right after this line:

[code]if(mail($recipient, $subject, $message, $headers))[/code]

So your code block should change from this:

[code]
<?php

if(mail($recipient, $subject, $message, $headers))

# send confirmation email
if($sendnotification == true) {
$notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly";
$notification_subject = "Thanks for your message to $websitetitle.";
mail($sender_email, $notification_subject, $notification_message, "From: $recipient");


        {
            header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success');
        }
        else
        {
            header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?failure');
        }
    }

?>
[/code]

... to this ...

[code]
<?php

if(mail($recipient, $subject, $message, $headers))
{
    # send confirmation email
    if($sendnotification == true)
    {
          $notification_message = "Thank you for contacting $websitetitle, $sender_name. We have received your email and will be in touch shortly";
          $notification_subject = "Thanks for your message to $websitetitle.";
          mail($sender_email, $notification_subject, $notification_message, "From: $recipient");

          header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?success');
    }
    else
    {
          header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?failure');
    }
}

?>
[/code]
Link to comment
Share on other sites

Alpine
Your code works, but I am still not sure what you have added/changed and why, I'll get my glasses and have another look! But thanks again, youve been a great help.

Heyray
Sorry your'e amendment errors

EDIT>> Found the missing bracket at the end!  but how did you find it? Did you work through the code line by line, or somehow find the error by using an application?
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.