[英]need php code for checkbox html form
*** *** EDIT
编辑 ** * ***
** * ***
I HAVE GOT IT WORKING. 我已经开始工作了。 Now I just help with one simple minor thing: How do I get the email to separate each checkbox selection with a Comma?
现在,我只帮您解决一件简单的小事:如何获取电子邮件,以逗号分隔每个复选框?
So instaed of: 因此:
Interested In: Hourly Sales Lead Generation B2B Sales 有兴趣:每小时销售潜在客户生成B2B销售
It should read: 它应显示为:
Interested In: Hourly Sales Lead Generation, B2B Sales 有兴趣:每小时销售线索生成,B2B销售
Thanks, Nathan 谢谢,内森
I just signed up on this site hoping to get some help making my form to send the checkbox selections with the email. 我刚刚在该站点上注册,希望获得一些帮助,使我的表单可以将复选框选择与电子邮件一起发送。
I'm pretty much a noob with php stuff, but using a lynda.com I got my contact form at least working - now I just need to get the checkbox selections to send with the email. 我对php东西非常了解,但是使用lynda.com可以使我的联系表至少可以正常工作-现在,我只需要选择复选框即可与电子邮件一起发送。
Here is my form: 这是我的表格:
<form id="mainForm" method="post" name="contact" action="http://www.digicrest.com/sales/contactFormProcess.php">
<ul>
<li>Name<span>*</span> <br/>
<input class="form2" name="name" type="text" />
</li>
<li>Email Address<span>*</span> <br/>
<input class="form2" name="email" type="text" />
</li>
<li>Subject<span>*</span> <br/>
<input class="form2" name="subject" type="text" />
</li>
<li>Company<span>*</span> <br/>
<input class="form2" name="company" type="text" />
</li>
<li>Phone Number <br/>
<input class="form2" name="phone" type="text" />
</li>
<li id="checkboxes">
I'm Interested In:
<br />
<div><input type="checkbox" name="interested[]" value="Pay For Performance Appointment Setting" class="checkbox" id="option1" /> Pay For Performance Appointment Setting
<br />
<input type="checkbox" name="interested[]" value="Hourly Sales Lead Generation" class="checkbox" id="option2" /> Hourly Sales Lead Generation
<br />
<input type="checkbox" name="interested[]" value="B2B Sales" class="checkbox" id="option3" /> B2B Sales
<br />
<input type="checkbox" name="interested[]" value="Inbound Lead Generation" class="checkbox" id="option4" /> Inbound Lead Generation
</div>
</li>
<li><span class="clearLeft"></span></li>
<div class="clearLeft"></div>
<li>Comments / Details<span>*</span> <br/>
<textarea class="form2B" cols="20" rows="6" name="message"></textarea>
</li>
</ul>
<div id="formButtons2">
<input class="clear2" type="reset" value=""/>
<input class="submit" type="image" name="submit" src="images/send-big.jpg" alt="" />
</div>
</form>
-and here is my php- -这是我的PHP-
<?php
/* Subject and Email Variables */
$emailSubject = $_POST['subject'];
$webMaster = 'info@digicrest.com';
$email = $_POST['email'];
/* Gathering Data Variables */
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$subjectField = $_POST['subject'];
$companyField = $_POST['company'];
$phone = $_POST['phone'];
$messageField = $_POST['message'];
$interestedField = implode(' ', $_POST['interested']);
$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Email: $emailField <br>
Subject: $subjectField <br>
Company: $companyField <br>
Phone: $phone <br>
Interested In: $interestedField <br>
Message: $messageField <br>
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
/* Results Rendered as HTML */
$theResults = <<<EOD
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome To Sales Source Lead Generation - B2B Solutions</title>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<link href='http://fonts.googleapis.com/css?family=Oswald:400,700' rel='stylesheet' type='text/css' />
<link rel="stylesheet" href="css/basic-jquery-slider.css" />
</head>
<body>
<!-- header -->
<div id="pageArea">
<div id="headerArea">
<div id="header">
<div class="content">
<a id="logo" href="index.html" title="Home"><img src="images/logo.jpg" width="198" height="102" alt=""/></a>
<div id="phone">
Call us Toll Free: <span>1-888-557-4223</span> Ext 2
</div>
<div id="navbar">
<div id="home"><a href="index.html" title="Home"></a></div>
<div> | </div>
<div id="about"><a href="about.html" title="About Us"></a></div>
<div> | </div>
<div id="services"><a href="services.html" title="Services"></a></div>
<div> | </div>
<div id="faq"><a href="faq.html" title="FAQ"></a></div>
<div> | </div>
<div id="rep"><a href="rep.html" title="Become a Rep"></a></div>
<div> | </div>
<div id="contact"><a href="contact.html" title="Contact Us"><img src="images/contact-current.jpg" width="87" height="16" alt="" /></a></div>
</div>
<div class="clearer"></div>
</div>
</div>
</div>
<div id="barArea2">
<div class="content">
<div id="light"><img src="images/light.png" width="787" height="493" alt="" /></div>
<h1 class="pageTitle">Message Sent!</h1>
</div>
</div>
<div id="hr"></div>
<div class="contentArea">
<div class="content">
<div align="center">Thank you for your message! Your email will be answered as soon as possible!
<br />
<br />
<a href="index.html" title="Home">Click To Go Back!</a>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
</body>
</html>
EOD;
echo "$theResults";
?>
I appreciate any help :) 感谢您的帮助:)
Thanks for reading. 谢谢阅读。
-Nathan -内森
In your case, your checkboxes are stored on the $_POST['interested']
array. 就您而言,您的复选框存储在
$_POST['interested']
数组中。 So you need to include the imploded form of them ( $interestedField
). 因此,您需要包括它们的内含形式(
$interestedField
)。
If you want the commas, simply implode by ", "
instead of " "
. 如果需要逗号,只需用
", "
代替" "
爆。
Assuming everything else is working: 假设其他一切正常:
/* Gathering Data Variables */
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$subjectField = $_POST['subject'];
$messageField = $_POST['message'];
$choices = implode(", ", $_POST['interested']);
$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Subject: $subjectField <br>
Email: $emailField <br>
Message: $messageField <br>
Choices: $choices
EOD;
Note that this will just give you the VALUES of the checkboxes (eg: 1, 2, 3, 4) and not the text (eg: "Pay For Performance Appointment Setting"). 请注意,这只会为您提供复选框的值(例如:1、2、3、4),而不是文本(例如:“为绩效预约设置付款”)。 To get this, you'd need to have those texts somewhere in your PHP:
为了做到这一点,您需要将这些文本放在PHP中的某个位置:
$labels = array("1"=>"Pay For Performance Appointment Setting",
"2"=>"Hourly Sales Lead Generation",
"3"=>"B2B Sales",
"4"=>"Inbound Lead Generation");
/* Gathering Data Variables */
$nameField = $_POST['name'];
$emailField = $_POST['email'];
$subjectField = $_POST['subject'];
$messageField = $_POST['message'];
$choices = array();
foreach($_POST['interested'] as $choice) {
$choices[] = $labels[$choice];
}
$choices = implode(", ", $choices);
$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Subject: $subjectField <br>
Email: $emailField <br>
Message: $messageField <br>
Choices: $choices
EOD;
Hope this helps. 希望这可以帮助。
EDIT: OK, you changed your post while I was answering so that the values of the checkboxes contained the text which makes the second part of my answer redundant. 编辑:好的,您在我回答时更改了帖子,以便复选框的值包含使我的回答的第二部分多余的文本。 If you wanted to save the data into a database, the best way would be to have numeric values, and the description stored somewhere else.
如果要将数据保存到数据库中,最好的方法是将数字值和描述存储在其他位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.