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.

Seraph00Author
Participant
January 21, 2010

The "isset" line is usually used when the mailing script is on the same page as the form itself. If the mailing script is in a separate page from the form, then you don't need it. That should not have stopped it from working though - unless you didn't have the submit button named "submit". To be honest, I think this line "$where_form_is="http:///".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));" would have actually stopped it - more than the "isset" line. I don't know where that came from - but as I look at it closer - it's not making any sense on it's own

When the script is on the same page as the form - then what the "isset" line does is stop the script from running - unless the form was submitted. So first visit, it would just display the form - after submitting the form it would run the script.

As a reminder,  don't forget to validate all that input! Email scripts are easy for hackers to abuse, so you will want to validate those POST values.

Other than that - Glad to hear you got it running.


Thanks again for all the help.  I'm new to PHP and want to learn more about validating the form.  Can you recomend a good resource to learn about PHP?