Skip to main content
Participant
January 14, 2010
Answered

PHP Formmail questions

  • January 14, 2010
  • 1 reply
  • 1736 views

I've set up the following php formmail code for a contact form on a website.  The form accepts the email, phone and question fields fine but I'm trying to add a "subscribe to email list" button.  Can anyone tell me the code to check if the box has been checked?

<?php

$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

mail("xxxxxxx@xxxxxxx.org","Website Contact Form submission","Form data:

Mathew, you have a question from: " . $_POST['name'] . "
Email: " . $_POST['email'] . "
Phone Number: " . $_POST['phone'] . "
Their question reads: " . $_POST['question'] . "

");

include("confirm.html");

?>

This topic has been closed for replies.
Correct answer

This code is great, thanks for the help but there is one problem.  I want users to have the option of subscribing to my email not forcing them too or giving them a message saying they have to subscribe to submit their info.  I'm trying to get the code to simply tell me wether or not the checkbox has been clicked so i can add them to my distribution list.

Thanks for the help thus far though!


Sorry, I misunderstood your problem/question.

1: Set the checkbox name to "subscribe" on the form, and make sure the submit name is set to "submit".

2: Set the checkbox "value when checked" to 1. (The user above mentioned a boolean check, and that works fine as well. I use a check value here as it makes the code very easy to understand)

Now try this idea:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$question = $_POST['question'];
if ($_POST['subscribe'] == 1) {
$subscribe = "Yes";
} else {
$subscirbe = "No";
}


if (isset($_POST['submit'])) {

$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

mail("xxxxxxx@xxxxxxx.org","Website Contact Form submission","Form data:
Mathew, you have a question from: $name
Email: $email
Phone Number: $phone
Their question reads: $question
Would like to subscribe: $subscribe
");

include("confirm.html");

}
?>

NOTE: I still include (and recommend) that you check to see if "submit" is set before processing the form, but I took out the "else" that said they had to subscribe.

1 reply

January 14, 2010

You could use the isset function, something like this should work:

if (isset($_POST['subscribe']))

Seraph00Author
Participant
January 14, 2010

Hmm, doesnt seem to be working.  Also, when i put the code in and test it the redirect to my confirmation page does not come up.  Instead it redirects directly to my php file and the message is not sent.

January 15, 2010

The page with the form on it has a submit button right? If you use the sample of "isset" provided, then that submit button must be named "subscribe"! Please verify that is the case.

With the "isset" added to your code, it should look something like this.

<?php
if (isset($_POST['subscribe'])) {

$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

mail("xxxxxxx@xxxxxxx.org","Website Contact Form submission","Form data:
Mathew, you have a question from: " . $_POST['name'] . "
Email: " . $_POST['email'] . "
Phone Number: " . $_POST['phone'] . "
Their question reads: " . $_POST['question'] . "
");

include("confirm.html");

} else {
echo "Sorry, you cannot come to this page without clicking the subscribe button";
}
?>

NOW - I still don't believe your code will work, as the message section opens and closes many times. You should gather your POST DATA before creating the email - then just enter in variables in the email. Like this:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$question = $_POST['question'];

if (isset($_POST['subscribe'])) {

$where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

mail("xxxxxxx@xxxxxxx.org","Website Contact Form submission","Form data:
Mathew, you have a question from: $name
Email: $email
Phone Number: $phone
Their question reads: $question
");

include("confirm.html");

} else {
echo "Sorry, you cannot come to this page without clicking the subscribe button";
}
?>

The other value to grabbing your post data and assigning it to variables, is that you can VALIDATE it at the same time. That is a step completely missing here - but really, really important.

Good luck!