php tick box array for email form - php

i'm new to stackoverflow but after trying a number of way i can't get my head around this email form. The form works fine but i've tried to add 4 optional tick boxes so the client can tick so we know which package they're interested in. I think i've got the front end setup properly:
<p><label for="author">Full Name:</label><br />
<input type="text" name="cf_name" class="details" tabindex="1" /></p>
<p><label for="telephone">Telephone:</label><br />
<input type="text" name="cf_telephone" class="details" tabindex="2" /></p>
<p><label for="email">E-mail:</label><br />
<input type="text" name="cf_email" class="details" tabindex="3" /></p>
<p><label for="package">Package Type:</label><br />
<input type="checkbox" name="cf_package[]" class="package" value="Business English Classes" tabindex="4" /> Business English Classes<br />
<input type="checkbox" name="cf_package[]" class="package" value="English Conversation Classes" tabindex="4" /> English Conversation Classes<br />
<input type="checkbox" name="cf_package[]" class="package" value="Exam Preparation Classes" tabindex="4" /> Exam Preparation Classes<br />
<input type="checkbox" name="cf_package[]" class="package" value="Writing Skills Classes" tabindex="4" /> Writing Skills Classes
</p>
<p><label for="message">Questions/Comments:</label><br />
<textarea type="text" name="cf_message" class="questions" tabindex="8"></textarea></p>
<p style="padding-top:10px;"><input type="submit" name="submit" value="Submit" class="button" tabindex="9" /> <input type="reset" name="submit" value="Clear" class="button" tabindex="10" /></p>
My backend script is as follows:
<code><?php
// get all values from form and remove spaces before/after values
$field_name = trim($_POST['cf_name']);
$field_telephone = trim($_POST['cf_telephone']);
$field_email = trim($_POST['cf_email']);
$field_package = trim($_POST['cf_package']);
$field_message = trim($_POST['cf_message']);
$mail_to = 'theenglishbeehive#gmail.com';
$subject = 'Enquiry from '.$field_name;
// check if user input own e-mail -> generate headers and send mail
if ($field_email && $field_message)
{
// generate body of message
$body_message = "From: ".$field_name."\n";
$body_message .= "Telephone: ".$field_telephone."\n";
$body_message .= "E-mail: ".$field_email."\n";
$body_message .= "Package Type: ".$field_package."\n";
$body_message .= "Questions/Comments: ".$field_message;
$headers = "From: ".$field_email."\n";
$headers .= "Reply-To: ".$field_email."\n";
$headers .= "Return-Path: ".$field_email."\n";
$headers .= "X-Priority: 3 (Normal)\n";
// send email
$mail_status = mail($mail_to, $subject, $body_message, $headers);
}
// success ?
if ($mail_status)
{
?>
<script language="javascript" type="text/javascript">
window.location = 'enquiry-sent.php';
</script>
<?php
}
else
{
?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to theenglishbeehive#gmail.com');
window.location = 'index.php';
</script>
<?php
}
?>
</code>
Hopefully i've got this showing properly on the website too.

The selected 'package' will be sent to the server as an array, not as a string, because you're using the array notation ([]) and using checkboxes, people can select multiple packages.
You might want to convert the array to a comma-separated string and show which packageS the user selected:
$field_package = implode(', ', $_POST['cf_package']);
and then:
$body_message .= "Package Type(s): ".$field_package."\n";
If only a single option should be allowed to be selected, you might consider using a radio-button in stead of checkboxes.

Related

PHP emails not sending

Been trying to get this working and sending to my email from the website but the emails aren't coming through.
It is probably something really easy. I also blocked my actual sending email (for both areas)
Thanks for any help!
My php code in index.php
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_message = $_POST['message'];
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
$mail_to = 'coverupemail#email.com';
$subject = 'Message from a portfolio visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$email."\r\n";
$headers .= 'Reply-To: '.$email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for the message.');
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email manually to coverupemail#email.com');
window.location = 'index.html';
</script>
<?php
}
?>
My HTML code linking to index.php
<form method="post" action="index.php">
<p class="contact">
<input type="text" name="name" id="name" value="" size="22" />
<label for="name"><small>Name (required)</small></label>
</p>
<p class="contact">
<input type="email" name="email" id="email" value="" size="22" />
<label for="email"><small>Mail (required)</small></label>
</p>
<p class="contact">
<textarea name="message" id="message" value="" rows="10"></textarea>
<label for="message"><small>Message (required)</small></label>
</p>
<p class="contact">
<input id="submit" name="submit" type="submit" value="Submit" />
<input name="reset" type="reset" id="reset" tabindex="5" value="Reset Form" />
</p>
</form>
What is the $mail_status value ? Even if its "1" it does not guarantee the sending of mail :( it then depends upon the server PHP returns 1 the moment the message is handed over to the mail sending part of the server.
Cases 1 : if the script is running on server then try a sample code to verify that actually mail are been sent create a page mailtest.php with the following code
<?php mail("coverupemail#email.com","Test Msg","Hello this is just test message");?>
this will verify that yes its working.
Case 2 : if you are working on a localhost then u will have to have some SMTP server or mail server to make it easy you can use gmail to send via your localhost ( i did this i use my gmail account )
Also see this link Email sending
btw
a. ini_set() i think is not used.
b. also window.location = 'index.html'; will just navigate to the index page wat if the person navigates back ? the mail wud be sent again maybe... so i wud suggest to use
location.replace('index.html');
Thx

HTML Form Submission via PHP

Attempting to submit an HTML5 form to email via PHP, with a redirect to a "Thank you!" page after a successful submission. Problem is, the form isn't sending and the redirect isn't happening.
Here's my HTML:
<form id="vendorInfo" action="process_form_vendor.php" method="post">
<label for="vendorName">Vendor Name:</label>
<br />
<input id="vendorName" name="vendorName" type="text" maxlength="30" required>
<br />
<label for="contactName">Contact Name:</label> <br />
<input id="contactName" name="contactName" type="text" maxlength="35" required>
<br />
<label for="vendorType">Organization Type:</label>
<br />
<select id="vendorType" name="vendorType">
<option value="carrier">
Insurance Carrier
</option>
<option value="tech_crm">
Technology/CRM Management
</option>
<option value="leadProvider">
Lead Provider
</option>
<option value="info_comm">
Information/Communication
</option>
<option value="other">
Other (please describe below)
</option>
</select>
<br />
<label for="other1">Other Organization Type:</label>
<br />
<input id="other1" name="other1" type="text" maxlength="25">
<br />
<label for="email">Email:</label>
<br />
<input id="email" name="email" type="email" maxlength="30" required>
<br />
<label for="phone">Phone:</label>
<br />
<input id="phone" name="phone" type="tel" maxlength="12" required placeholder="xxx-xxx-xxxx">
<br />
<label for="questions">Any questions or comments? Leave them here:</label>
<br />
<textarea id="questions" name="questions" rows="10" maxlength="300"></textarea>
<br />
<br />
<fieldset id="selectionBox">
<legend id="packageSelect">
The following sponsorship packages are available for the Sales Summit; contact <ahref="mailto:email#domain.com”>Amanda</a> for pricing and details:
</legend>
<input type="radio" name="packageSelect" value="Bronze Package" checked> Bronze
<br />
<br />
<input type="radio" name="packageSelect" value="Silver Package"> Silver
<br />
<br />
<input type="radio" name="packageSelect" value="Gold Lunch Package"> Gold (breakfast; exclusive sponsorship)
<br />
<br />
<input type="radio" name="packageSelect" value="Gold Breakfast Package"> Gold (lunch; exclusive sponsorship)
<br />
<br />
<input type="radio" name="packageSelect" value="Gold Trade Show Package"> Gold (trade show; exclusive sponsorship)
</fieldset>
<br />
<button type="submit" name="submit">Submit</button> <button type="reset" name="reset">Reset</button>
<br />
</form>
And here is my PHP:
<?php
if(!isset($_POST['submit']))
{
echo "error; you need to submit the form!";
}
$vendorName = $_POST['vendorName'];
$contactName = $_POST['contactName'];
$vendorType = $_POST['vendorType'];
$other1 = $_POST['other1'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$questions = $_POST['questions'];
$packageSelect = $_POST['packageSelect'];
if (empty($vendorName)||(empty($contactName)||(empty($vendorType)||(empty($email)||(empty($phone)||(empty($packageSelect)) {
echo "Vendor Name, Contact Name, Vendor Type, Email, Phone, and Package Selection are mandatory!";
exit;
}
$email_from = 'email#domain.net';
$email_subject = '2014 SMS Sales Summit - New Vendor Reservation Request';
$email_body = "You have received a new vendor reservation request for the 2014 SMS Sales Summit from $contactName at $vendorName.\n".
"Vendor Type: $vendorType\n".
"Other Vendor Type: $other1\n".
"Email Address: $email\n".
"Phone Number: $phone\n".
"Additional Questions: $questions\n".
"Sponsorship Level: $packageSelect\n".
$to = 'email#domain.net';
$headers = "$email_from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
header('Location: thank-you.html');
?>
I have no idea what's going on or why this isn't working. Any ideas?
(Tested) Give this a try, it worked for me.
Plus, you may get an error saying "headers already sent", which did for me, so I used an echo at the end and I commented your header(".... to test with. If you have a space before <?php this could cause the error message to appear. You can try using ob_start(); just below your opening PHP tag.
Your empty conditionals ) had some too many, and some missing/not at the right spot.
Plus, a missing closing semi-colon at the end of "Sponsorship Level: $packageSelect\n". where there was a dot. Plus a missing From: which has been added.
<?php
// uncomment line below to use with header redirect
// ob_start();
if(!isset($_POST['submit']))
{
echo "error you need to submit the form!";
}
$vendorName = $_POST['vendorName'];
$contactName = $_POST['contactName'];
$vendorType = $_POST['vendorType'];
$other1 = $_POST['other1'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$questions = $_POST['questions'];
$packageSelect = $_POST['packageSelect'];
if (empty($vendorName)|| empty($contactName)|| empty($vendorType)|| empty($email)|| empty($phone)|| empty($packageSelect)){
echo "Vendor Name, Contact Name, Vendor Type, Email, Phone, and Package Selection are mandatory!";
exit;
}
$email_from = 'email#domain.net';
$email_subject = '2014 SMS Sales Summit - New Vendor Reservation Request';
$email_body = "You have received a new vendor reservation request for the 2014 SMS Sales Summit from $contactName at $vendorName.\n".
"Vendor Type: $vendorType\n".
"Other Vendor Type: $other1\n".
"Email Address: $email\n".
"Phone Number: $phone\n".
"Additional Questions: $questions\n".
"Sponsorship Level: $packageSelect\n";
$to = "email#domain.net";
$headers = 'From: ' . $email_from . "\r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
// header('Location: thank-you.html');
echo "thanks";
?>
Footnotes:
If it still does not "send", then change:
<button type="submit" name="submit">Submit</button>
to:
<input type="submit" name="submit" value="Submit">
If you use the header("... along with the ob_start(); you must not use the echo below it. Just comment that out.
You have syntax error in $email_from and $to. Need to use '(single quotes) or " (double quotes) instead of ‘ . Try this,
$email_from = 'email#domain.net';
...^ ^....
instead of
$email_from = ‘email#domain.net’;
Also, in your if condition you have missed to add lots of )
if (empty($vendorName)||
empty($contactName)||
empty($vendorType)||
empty($email)||
empty($phone)||
empty($packageSelect) ) {
echo "Vendor Name, Contact Name, Vendor Type, Email, Phone, and Package Selection are mandatory!";
exit;
}
<button type="submit&" name="submit">Submit</button> <button type="reset" name="reset">Reset</button>
to
<button type="submit" name="submit">Submit</button> <button type="reset" name="reset">Reset</button>
Is the form submitting from frontend?
also in php code, the $email_from:
$email_from = ‘email#domain.net’;
change it to:
$email_from = 'email#domain.net';
note the difference in single quotes. Same goes for $to:
$to = ‘email#domain.net';
change it to:
$to = 'email#domain.net';
Can you remove the "&" in <button type ="submit&">?
You also need to close the form using </form> after the last line
Why the & in type="submit&"? Also, there is no closing </form> tag.
you forgot to put a semicolon after $email_body
please put one
$email_body = "";

Variable not working in mail form?

I have a HTML form with 4 id (name, email, message, subject), a js with all the variables declared and a PHP that should send the mail.
HTML
<form id="formail" method="post" action="">
<input type="text" id="nome" name="nome" value="" size="22" /><br />
<input type="text" id="email" name="email" value="" size="54" /><br />
<textarea id="messaggio" name="messaggio" rows="1" cols="55" style="resize: none;"></textarea><br />
<input type="text" id="subject" name="subject" value="" size="22" /><br />
<input type="submit" id="send" name="send" value="" style="cursor: pointer"/>
<br />
<div id="answer"></div>
</form>
This is the js
var valid = '';
var isr = ' requested.</h6>';
var name = $("#nome").val();
var mail = $("#email").val();
var subject = $("#subject").val();
var messaggio = $("#messaggio").val();
(follow controls about the name and mail, and the send function)
This is the php
$mail = trim($_POST['mail']);
$name = $_POST['name'];
$text = $_POST['messaggio'];
$subject = $_POST['subject'];
$ip = $_SERVER['REMOTE_ADDR'];
$to = "admin#test.com";
$message = "Username: ".$name.", ".$mail.".<br />";
$message .= "Subject: ".$subject.".<br />";
$message .= "Messaggio: <br />".$text."<br /><br />";
$message .= "IP: ".$ip."<br />";
$headers = "From: ".$mail." \r\n";
$headers .= "Reply-To: ".$mail." \r\n";
$headers .= "MIME-Version: 1.0 \n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1 \n";
if(mail($to, $subject, $message, $headers)){
echo "<h6>Message sent!</h6>";
}
I posted only the relevant code. When I click send I'll receive the mail, however the field "subject" is blank, as if the variables "subject" had been ignored.
Could you please help me? I'm starting to learn PHP but I'm still a newbie. Thank you.
Where is your mail function?
use following mail function:
mail($to-mail,$subject,$message,$header);
Two key points here:
a) Not sure exactly what function you're using to actually send the mail, but assuming PHP mail() you will need to use the $subject as the second parameter.
b) If you publish this on the open web you will be exploited as an open relay by spammers.
The attack you are vulnerable to is called 'header injection'.
In short, if I submit my 'mail' value as myemail#example.org%0ABcc:poorsap#example.com the script will add an extra line (the %0A is a linefeed), and submit to mail() with an extra Bcc header for however many emails I like (in this case poorsap#example.org).
Please take a read through this: http://www.securephpwiki.com/index.php/Email_Injection and consider using an alternate library to avoid this problem.

PHP mail form not working

I am making a feedback form for my website using php, I thought that it was working, but then it took an arrow to the code.
Anyways, the form has 4 fields: "name", "email", "subject", and "message".
I get the "Message failed" alert if all four fields have content.
Seeing as that is the case, I tested with content in 3 of the 4 fields, which gives me 4 combinations:
combo 1 - "name", "email", "subject"
combo 2 - "name", "email", "message"
combo 3 - "name", "subject", "message"
combo 4 - "email", "subject", "message"
The results were as follows:
combo 1 - "Thank you for your message."
combo 2 - "Thank you for your message."
combo 3 - "Message failed."
combo 4 - "Thank you for your message."
Here is the html code I am using:
<form action="contact.php" method="post">
<input type="hidden" name="page" value="contact" />
<input type="hidden" name="req" value="submit" />
Your Name: <input type="text" name="name" />
<br/>
Your Email: <input type="text" name="email" />
<br/>
Subject: <input type="text" name="subject" size="69" />
<br/>
Message:
<br/>
<textarea cols="63" rows="8" name="message"></textarea>
</br>
<input type="submit" value="Send" />
<input type="reset" value="Clear" />
</form>
And here is contact.php:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
$mail_to = 'email#my_website.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '."\n";
$body_message .= $field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for your message.');
window.location = './contact.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed.');
window.location = './contact.html';
</script>
<?php
}
?>
Is there anything wrong with the code? I don't understand why it is doing this.
Great answer from Anton but I wanted to dig a little bit more into this and expand his answer.
If you want to make sure someone sent the email, require it hardcoded with php (and html5 would be cool too, but this can be avoid so you still need php). I also deleted the javascript bit as it felt irrelevant there. This is what I would try and since it's short I'd do it all in the same page. There are many subtle changes:
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
//If all required fields are filled
if (!empty($field_name)&&!empty($field_email)&&!empty($field_message))
{
$mail_to = 'email#my_website.com';
$subject = 'Message from a site visitor: '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Subject: '.$field_subject."\n";
$body_message .= 'Message: '."\n";
$body_message .= $field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
echo "Thank you for your message.";
}
//If not all required fields are filled display form again.
else
{ ?>
Please fill all the required fields.<br>
<form action="" method="post">
<input type="hidden" name="page" value="contact" />
<input type="hidden" name="req" value="submit" />
<?php //The php bits are to retrieve the valid fields ?>
Your Name*: <input required type="text" value="<?php echo $field_name; ?>" name="name"/>
<br/>
Your Email*: <input required type="email" value="<?php echo $field_email; ?>" name="email" />
<br/>
Subject: <input type="text" name="subject" value="<?php echo $field_subject; ?>" size="69" />
<br/>
Message*:
<br/>
<textarea required cols="63" rows="8" name="message"><?php echo $field_message; ?></textarea>
</br>
<input type="submit" value="Send" />
<input type="reset" value="Clear" />
</form>
<?php
}
?>
Otherwise, if you don't care about who sends it, you could just put something like this
$field_name = $_POST['name'];
$field_email = $_POST['email'];
if (empty($field_email)) $field_email="anonymous#MYPAGE.com";
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];
Last thing, I hope this is just a pseudo code as it doesn't comply almost any html rule. You need < html > tags, < body > etc...
Just tested and worked great (;
The problem is probably this:
Quote from http://php.net/manual/en/function.mail.php
When sending mail, the mail must contain a From header. This can be
set with the additional_headers parameter, or a default can be set in
php.ini.
Failing to do this will result in an error message similar to Warning:
mail(): "sendmail_from" not set in php.ini or custom "From:" header
missing. The From header sets also Return-Path under Windows.
In other words, if you do not have a header containing a proper "From: adress#email.com" an error will occur.

Issue with a PHP Online Order Form

I've created a contact form for a website with multiple fields. The clear button works perfectly, but when I click the Submit button, I inadvertently download the .php file associated with the webpage, instead of sending an email with the order form to the specified address. I'm really new to php here, so any help would be greatly appreciated.
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_quantity = $_POST['cf_quantity'];
$field_which = $_POST['cf_which'];
$field_size = $_POST['cf_size'];
$field_school = $_POST['cf_school'];
$field_college = $_POST['cf_college'];
$field_message = $_POST['cf_message'];
$mail_to = 'xxx#gmail.com';
$subject = 'TShirt Order - xxx.Org '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail Address: '.$field_email."\n";
$body_message .= 'Quantity of Shirts: '.$field_quantity."\n";
$body_message .= 'Shirt Style(s): '.field_which."\n";
$body_message .= 'Size(s): '.field_size."\n";
$body_message .= 'School: '.field_school."\n";
$body_message .= 'College: '.field_college."\n";
$body_message .= 'Message: '.$field_message;
$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";
$mail_status = mail($mail_to, $subject, $body_message, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Thank you for placing an order with xxx.Org. We will contact you shortly.');
window.location = 'contact_page.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to xxx#gmail.com');
window.location = 'contact_page.html';
</script>
<?php
}
?>
the html code is:
<form action="contact.php" method="post">
Full Name:
<input type="text" name="cf_name">
<br />
<br />
Email Address:
<input type="text" name="cf_email">
<br />
<br />
Quantity of TShirts:
<input type="numeric" name="cf_quantity">
<br />
<br />
Which Style(s)?
<input type="text" name="cf_which">
<br />
<br />
Size(s):
<input type="text" name="cf_size">
<br />
<br />
Name of School:
<input type="text" name="cf_school">
<br />
<br />
Name of College:
<input type="text" name="cf_college">
<br />
<br />
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
Are you completely sure the server accepts php? Seams to me the server doesn't understand it should use the php interpeter and instead of activating the php commands it just lets you download the file.
If you're not sure try and make a file with a simple in it, call that and see if it works.
best regards,
Mark

Categories