Help with Contact Form

Hi, I am having a similar problem, so I thought I’d ask for any tips here. My contact form doesn’t throw up an error, but nothing happens. We have a simple webpage at www.rapidresults.wang (Don’t laugh, we are just beginners!)

This the php file send_mail.php

[php]#!/usr/bin/php

<?php /* This first bit sets the email address that you want the form to be submitted to. You will need to change this value to a valid email address that you can access. */ $webmaster_email = "[email protected]"; /* This bit sets the URLs of the supporting pages. If you change the names of any of the pages, you will need to change the values here. */ $feedback_page = "send_us_a_message.html"; $error_page = "error_message.html"; $thankyou_page = "thank_you.html"; /* This next bit loads the form field data into variables. If you add a form field, you will need to add it here. */ $email_address = $_REQUEST['email_address'] ; $comments = $_REQUEST['comments'] ; /* The following function checks for email injection. Specifically, it checks for carriage returns - typically used by spammers to inject a CC list. */ function isInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } // If the user tries to access this script directly, redirect them to the feedback form, if (!isset($_REQUEST['email_address'])) { header( "Location: $feedback_page" ); } // If the form fields are empty, redirect to the error page. elseif (empty($email_address) || empty($comments)) { header( "Location: $error_page" ); } // If email injection is detected, redirect to the error page. elseif ( isInjected($email_address) ) { header( "Location: $error_page" ); } // If we passed all previous tests, send the email then redirect to the thank you page. else { mail( "$webmaster_email", "Feedback Form Results", $comments, "From: $email_address" ); header( "Location: $thankyou_page" ); } ?>[/php]

Lines 21 and 22, these 2 below are causing a problem.

$email_address = $_REQUEST['email_address'] ; $comments = $_REQUEST['comments'] ;

When I view the source file send_mail.php in Firefox I see this:

PHP Notice: Undefined index: email_address in D:\freehost\fa5d40d2\web\send_mail.php on line 21
PHP Notice: Undefined index: comments in D:\freehost\fa5d40d2\web\send_mail.php on line 22

As far as I can see, these variables are in the send_us_a_message.html here: name=“email_address” and name=“comments”
but it is not working. Any tips please??
If you have another, better, more elegant routine to do this, maybe you would be so kind as to share it with me.

Thanks!

This is the relevant part of send_us_a_message.html

You can send us a message here.

Your Email Adress:
Comments: Write your message here.
 

Not sure where to start… So many issues with this code. First, just because you expect the posted data to contain data, it may not! Therefore, you should check it before you to attempt to view it’s data. You can use the ISSET function to see if it is set.

Loosely like this: if (isset($_POST[“email_address”]) { handle it } else (throw error message about no address entered }

Next, in your email code, you did not set up any headers for the mail() function. In general, this is how it is done:
$to = ‘[email protected]’;
$subject = ‘the subject’;
$message = ‘hello’;
$headers = ‘From: [email protected]’ . “\r\n” .
‘Reply-To: [email protected]’ . “\r\n” .
‘X-Mailer: PHP/’ . phpversion();

mail($to, $subject, $message, $headers);

Note: Some servers do not need the headers, but, from my experience they are almost always needed…

Lastly, your form input are being sanitized using slightly odd code. You might want to look into the “FILTER_INPUT()” function which has options to sanitize the inputs from posted forms.

I normally just just one single page for contact forms. I post errors on the page above the input fields and copy the inputs and display them back to the user. In this manner, if they enter a lot of data or a long message, they do not loose it all if they forgot to enter their email address. Also, once sent, they can resend it if they need to change something they forgot about.

Hope this helps…

Thanks, I am completely new to this, so I don’t really get it yet. It still does not send a mail.

I changed send_mail.php like this:

/*
This first bit sets the email address that you want the form to be submitted to.
You will need to change this value to a valid email address that you can access.

[quote]*/
$webmaster_email = ‘[email protected]’;
$subject = ‘contact form reply’;

$headers = ‘From: $email_address’ . “\r\n” .
‘Reply-To: $email_address’ . “\r\n” .
‘X-Mailer: PHP/’ . phpversion();/quote]

And at the bottom of send_mail.php I put:

// If we passed all previous tests, send the email then redirect to the thank you page. else { mail($webmaster_email, $subject, $comments, $headers); //header( Location: $thankyou_page ); }

The problem I have is still have is:
[php]PHP Notice: Undefined index: email_address in D:\freehost\fa5d40d2\web\send_mail.php on line 26
PHP Notice: Undefined index: comments in D:\freehost\fa5d40d2\web\send_mail.php on line 27
[/php]

These are lines 26 and 27

/* This next bit loads the form field data into variables. If you add a form field, you will need to add it here. */ $email_address = $_REQUEST['email_address'] ; $comments = $_REQUEST['comments'] ;

email_address is the email address that the user puts in the first little box of the contact form page.
comments is the name of the box for the comments.

How can I explicitly tell send_mail.php what and where these are? Or is it better just to look for a better feedback form solution?

These variables are the input in the send_us_a_message.html here: name=“email_address” and name=“comments”

Well, since you are sending your data via form post, you need to make sure it is send otherwise, no data is in the posted array of data. Normally, I would do it this way…

Change the submit button to have a name:
Then, in the PHP file do something like:
if (isset($_POST[“submit”])) {
// do all code here…
}
And, in the code section do something like:
if (isset($_POST[“email_address”])) {
filter_input(INPUT_POST, “email_address”);
} else {
$error_message = “You must enter your email address…”;
}

In other words, you need to first check if the user pressed the submit button and also if they enter the email address.
Here is a full tutorial that might help you: http://www.w3schools.com/php/php_forms.asp
Just press the green Next-Chapter and walk thru all the parts. It gives a lot of info and will save me giving the to you.
Hope it helps…

Change the submit button to have a name: Then, in the PHP file do something like: if (isset($_POST["submit"])) {

No, that will completely fail in certain circumstances and will be a problem in others.

The correct way is to use if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’)

Thanks for your advice, I appreciate it. I will go through the web site you mentioned, see if I can make it work. Can’t be rocket science, can it??

Sunday morning, I have time so I tried a different tack: Went to this site, generated the feedback form

http://www.thesitewizard.com/wizards/feedbackform.php

Saved it, altered my send_us_a_message.html

However, if I check the source code of send_us_a_message.html in Firefox and then click on feedback.php, I still see this:

PHP Notice: Undefined index: name in D:\freehost\fa5d40d2\web\feedback.php on line 92
PHP Notice: Undefined index: email in D:\freehost\fa5d40d2\web\feedback.php on line 93
PHP Notice: Undefined index: comments in D:\freehost\fa5d40d2\web\feedback.php on line 94

How do I tell php what these variables are???

Well, an undefined index means that it was never assigned. Or, you spelled it differently. If you spell the email address in
one part of your code as “email_address” and then attempt to access it using '$_POST[“email”]; then it is an undefined
variable. Do you understand that?

You can also get an “undefined index” if a form does not submit the variable you are looking for. That is where you must test
if the variable exists before you access it. ( Using isset() function. )

That ‘undefined index’ seems to be the problem from the start.

These are lines 92, 93 and 94 from feedback.php

$fullname = isset($_POST['fullname']) ? $_POST['fullname'] : $_POST['name'] ; $email = $_POST['email'] ; $comments = $_POST['comments'] ;

Do they not define those variables correctly???

edit
I am using the feedback.php from http://www.thesitewizard.com/wizards/feedbackform.php now.

This is the form part of my send_us_a_message.html

Name
 
Email
 
Comments
 
Write your message here.
 

As you can see, it has
name=“fullname”
name=“email”
name=“comments”

but these are not being picked up.

You have a very odd issue. To start the debugging process, add the following to the script that processes the form:

[php]<?php
if ( $_SERVER[‘REQUEST_METHOD’] == ‘POST’){
echo “

”;
print_r( $_POST );
echo “
”;
exit();
} else {
echo ‘

Form not submitted

’;
}[/php]

Thanks for that!

I did as you said. This is what I added to feedback.php and re-uploaded with Filezilla.

/* The following added on advice from phphelp.com */ if ( $_SERVER['REQUEST_METHOD'] == 'POST'){ echo "
";
     print_r( $_POST );
     echo "
"; exit(); } else { echo '

Form not submitted

'; } /*end of added portion*/

This is what happens now when I click submit: I end up at www.rapidresults.wang/feedback.php and I see this (I changed my email, but it is correct in the real world):

Array ( [fullname] => Pedro Rodriguez [email] => [email protected] [comments] => Hi me! )

It seems to be seeing what it should see.

Quo vadis?

Now that we know it is getting the proper values change it to this:[php]

if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {

$yourEmail = '[email protected]';
$subject = 'the subject';
$headers = "From: [email protected]\r\nReply-To: [email protected]\r\nX-Mailer: PHP/" . phpversion();
echo "<pre>";
print_r($_POST);
echo "</pre>";
$body = '';
foreach( $_POST as $k => $v ){
    $v = strlen($v) > 0 ? $v : 'NULL';
    $body .= "$k: $v\r\n";
}

if ( mail($yourEmail,$subject,$body, $headers) ){
    echo "<p>Message sent.</p>";
} else {
    echo "<p>There was a problem sending the message.</p>";
}

}[/php]

Now, keep in mind that the mail function is not reliable and the message will LIKELY end up in you spam folder, if it is accepted at all.

Ok, thanks again.

I changed the inserted part to the above. I changed the email to our contact email, which is also our webmaster email, so I changed [email protected] to [email protected] (but our real email).

Now I don’t get to www.rapidresults.wang/feedback.php I get nothing, no error, no message ‘Message sent.’, no thank_you.html and no email.

When I now right click on the page send_us_a_messagel.html and click ‘view page source’ then click on feedback.php I see the same fault again:

PHP Notice: Undefined index: name in D:\freehost\fa5d40d2\web\feedback.php on line 92 PHP Notice: Undefined index: email in D:\freehost\fa5d40d2\web\feedback.php on line 93 PHP Notice: Undefined index: comments in D:\freehost\fa5d40d2\web\feedback.php on line 94

These are those lines again:

92 $fullname = isset($_POST[‘fullname’]) ? $_POST[‘fullname’] : $_POST[‘name’] ;
93 $email = $_POST[‘email’] ;
94 $comments = $_POST[‘comments’] ;

Any ideas what may be the problem?

Those aren’t the errors I see. I do see the issue however.

Is this a Windows server? I suspect it is. If you have to use it, I would use a mail library like PHPMailer. Or you can look into setting up the mail server on a Windows machine.

These are the issues I see,

PHP Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in D:\freehost\fa5d40d2\web\feedback.php on line 165 PHP Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in D:\freehost\fa5d40d2\web\feedback.php on line 177

Thanks again.

I was beginning to suspect it might be a sever side problem. I’ll look at their web page, see if I can figure it out.

Thanks for your help!

Sponsor our Newsletter | Privacy Policy | Terms of Service