复选框和表单以及php邮件

时间:2014-09-07 03:13:56

标签: php forms checkbox

我有一个表单,它有一个发布的PHP脚本。 在表格中,我添加了多个由两个不同ID(“信息”和“问题”)分隔的复选框

<form action='../php/email.php' id='form' name='form'>
    <div class='form-group'>
        <input class='form-control' id='name' name='name' placeholder='Name' type='text'>
        <span class='glyphicon glyphicon-remove form-control-feedback'></span>
    </div>
    <div class='form-group'>
        <input class='form-control' id='email' name='email' placeholder='E-mail' type='text'>
        <span class='glyphicon glyphicon-remove form-control-feedback'></span>
    </div>
    <div class='form-group'>
        <input class='form-control' id='phone' name='phone' placeholder='Phone' type='text'>
    </div>
    <div class='form-group'>
        <textarea class='form-control' id='message' name='message' placeholder='Message'></textarea>
        <span class='glyphicon glyphicon-remove form-control-feedback'></span>
    </div>
    <div class='col-md-6 service'><strong>Would you like more information about:</strong>
        <div class='form-group'>
            <input type="checkbox" value="Assessment Tools" id="info" name="Assessment Tools" />
            <label for="Assessment Tools"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Assessment Tools 
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Supervisory and Management Development" id="info" name="Supervisory and Management Development" />
            <label for="Supervisory and Management Development"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Supervisory and Management Development
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Employee Training and Development" id="info" name="Employee Training and Development" />
            <label for="Employee Training and Development"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Employee Training and Development
        </div>
        <div class='form-group'>
            <input type="checkbox" value="ELearning" id="info" name="ELearning" />
            <label for="E-Learning"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            E-Learning
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Policies and Procedures" id="info" name="Policies and Procedures" />
            <label for="Policies and Procedures"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Policies and Procedures
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Employee Surveys" id="info" name="Employee Surveys" />
            <label for="Employee Surveys"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Employee Surveys
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Team Building" id="info" name="Team Building" />
            <label for="Team Building"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Team Building
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Career Development" id="info" name="Career Development" />
            <label for="Career Development"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Career Development
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Succession Managemen" id="info" name="Succession Managemen" />
            <label for="Succession Management"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Succession Management
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Recruitment and Selection" id="info" name="Recruitment and Selection" />
            <label for="Recruitment and Selection"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Recruitment and Selection
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Outsourced Services" id="info" name="Outsourced Services" />
            <label for="Outsourced Services"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Outsourced Services
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Leadership Coaching" id="info" name="Leadership Coaching" />
            <label for="Leadership Coaching"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Leadership Coaching
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Strategy Development" id="info" name="Strategy Development" />
            <label for="Strategy Development"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Strategy Development
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Performance Management" id="info" name="Performance Management" />
            <label for="Performance Management"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Performance Management
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Change Management" id="info" name="Change Management" />
            <label for="Change Management"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Change Management
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Outplacement Services" id="info" name="Outplacement Services" />
            <label for=" Outplacement Services"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Outplacement Services
        </div>
    </div>
    <div class='col-md-5 service'>
        <strong>Do you have a question about:</strong>
        <div class='form-group'>
            <input type="checkbox" value="Selection and Hiring for Fit" id="question" name="Selection and Hiring for Fit" />
            <label for="Selection and Hiring for Fit"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Selection and Hiring for "Fit" 
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Managing Talent" id="question" name="Managing Talent" />
            <label for="Managing Talent"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Managing Talent 
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Developing Leadership" id="question" name="Developing Leadership" />
            <label for="Developing Leadership"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Developing Leadership
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Improving Employee Productivity" id="question" name="Improving Employee Productivity" />
            <label for="Improving Employee Productivity"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Improving Employee Productivity
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Engaging the Workforce" id="question" name="Engaging the Workforce" />
            <label for="Engaging the Workforce"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Engaging the Workforce
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Increasing Sales Effectiveness" id="question" name="Increasing Sales Effectiveness" />
            <label for="Increasing Sales Effectiveness"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Increasing Sales Effectiveness
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Improving Customer Service" id="question" name="Improving Customer Service" />
            <label for="Improving Customer Service"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Improving Customer Service
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Reducing Risk - HR Compliance" id="question" name="Reducing Risk - HR Compliance" />
            <label for="Reducing Risk - HR Compliance"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Reducing Risk - HR Compliance
        </div>
    </div>
    <input class='form-control' id='send' type='submit' value='Send'>
</form>

PHP表单看起来像这样:

<?php
$subject = 'MTMPS Website Form Submission';
$to      = 'mike@iwillfillinlater.com';
$headers = 'MIME-Version: 1.0' . "\r\n" .
           'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = 'Name: ' . $_REQUEST['name'] . ' <br/>' .
           'E-mail: ' . $_REQUEST['email'] . ' <br/>' .
           'Phone: ' . $_REQUEST['phone'] . ' <br/>' .
           'Message: ' .$_REQUEST['message'] . ' <br/>' .
           'Requesting Information On: ' . $_REQUEST['info'] . ' <br/>' .
           'Have a Question About: ' . $_REQUEST['question'] . ' <br/>';
if (@mail($to, $subject, $message, $headers)) {
    echo 'Your Message has been sent, Thank you!';
} else {
    echo 'Oops there was a problem and your message was not sent';
}
?>

我对如何正确格式化最后2个请求感到茫然。我很乐意得到一些帮助,因为我确信它不会在当前状态下起作用。特别是如果它为每个请求发回多个答案。

1 个答案:

答案 0 :(得分:0)

我继续并重构了你的代码。现在应该工作到表单值(没有测试邮件发送功能)。 请注意,我仅使用名称字段,使用具有相同ID的多个DOM元素是不好的做法。

'技巧'是当使用具有相同名称和方括号的多个复选框时,PHP将其作为数组接收。即您可以使用具有不同值但名称相同的多个复选框:

<input type="checkbox" value="value1" name="my_checkbox[]">

<input type="checkbox" value="value2" name="my_checkbox[]">

您的HTML标记应为:

<form action='../php/email.php' id='form' name='form'>
    <div class='form-group'>
        <input class='form-control' id='name' name='name' placeholder='Name' type='text'>
        <span class='glyphicon glyphicon-remove form-control-feedback'></span>
    </div>
    <div class='form-group'>
        <input class='form-control' id='email' name='email' placeholder='E-mail' type='text'>
        <span class='glyphicon glyphicon-remove form-control-feedback'></span>
    </div>
    <div class='form-group'>
        <input class='form-control' id='phone' name='phone' placeholder='Phone' type='text'>
    </div>
    <div class='form-group'>
        <textarea class='form-control' id='message' name='message' placeholder='Message'></textarea>
        <span class='glyphicon glyphicon-remove form-control-feedback'></span>
    </div>
    <div class='col-md-6 service'><strong>Would you like more information about:</strong>
        <div class='form-group'>
            <input type="checkbox" value="Assessment Tools" name="more_info[]" />
            <label for="Assessment Tools"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Assessment Tools 
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Supervisory and Management Development"name="more_info[]" />
            <label for="Supervisory and Management Development"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Supervisory and Management Development
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Employee Training and Development" name="more_info[]" />
            <label for="Employee Training and Development"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Employee Training and Development
        </div>
        <div class='form-group'>
            <input type="checkbox" value="ELearning" name="more_info[]" />
            <label for="E-Learning"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            E-Learning
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Policies and Procedures" name="more_info[]" />
            <label for="Policies and Procedures"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Policies and Procedures
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Employee Surveys" name="more_info[]" />
            <label for="Employee Surveys"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Employee Surveys
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Team Building" name="more_info[]" />
            <label for="Team Building"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Team Building
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Career Development" name="more_info[]" />
            <label for="Career Development"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Career Development
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Succession Managemen" name="more_info[]" />
            <label for="Succession Management"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Succession Management
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Recruitment and Selection" name="more_info[]" />
            <label for="Recruitment and Selection"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Recruitment and Selection
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Outsourced Services" name="more_info[]"/>
            <label for="Outsourced Services"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Outsourced Services
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Leadership Coaching" name="more_info[]" />
            <label for="Leadership Coaching"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Leadership Coaching
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Strategy Development" name="more_info[]" />
            <label for="Strategy Development"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Strategy Development
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Performance Management" name="more_info[]" />
            <label for="Performance Management"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Performance Management
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Change Management" name="more_info[]"/>
            <label for="Change Management"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Change Management
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Outplacement Services" name="more_info[]" />
            <label for=" Outplacement Services"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Outplacement Services
        </div>
    </div>
    <div class='col-md-5 service'>
        <strong>Do you have a question about:</strong>
        <div class='form-group'>
            <input type="checkbox" value="Selection and Hiring for Fit" name="questions[]"/>
            <label for="Selection and Hiring for Fit"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Selection and Hiring for "Fit" 
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Managing Talent" id="question" name="questions[]" />
            <label for="Managing Talent"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Managing Talent 
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Developing Leadership" name="questions[]"/>
            <label for="Developing Leadership"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Developing Leadership
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Improving Employee Productivity" name="questions[]" />
            <label for="Improving Employee Productivity"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Improving Employee Productivity
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Engaging the Workforce" name="questions[]" />
            <label for="Engaging the Workforce"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Engaging the Workforce
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Increasing Sales Effectiveness" name="questions[]" />
            <label for="Increasing Sales Effectiveness"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Increasing Sales Effectiveness
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Improving Customer Service" name="questions[]" />
            <label for="Improving Customer Service"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Improving Customer Service
        </div>
        <div class='form-group'>
            <input type="checkbox" value="Reducing Risk - HR Compliance" name="questions[]" />
            <label for="Reducing Risk - HR Compliance"></label>
            <span class='glyphicon glyphicon-remove form-control-feedback'></span>
            Reducing Risk - HR Compliance
        </div>
    </div>
    <input class='form-control' id='send' type='submit' value='Send'>

你的PHP脚本应该是:

<?php

$more_info = isset( $_REQUEST['more_info']) ? implode(', ', $_REQUEST['more_info']) : 'no info boxes selected';
$questions = isset($_REQUEST['questions']) ? implode(', ', $_REQUEST['questions']) : 'no questions selected';

$subject = 'MTMPS Website Form Submission';
$to      = 'mike@iwillfillinlater.com';
$headers = 'MIME-Version: 1.0' . "\r\n" .
           'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$message = 'Name: ' . $_REQUEST['name'] . ' <br/>' .
           'E-mail: ' . $_REQUEST['email'] . ' <br/>' .
           'Phone: ' . $_REQUEST['phone'] . ' <br/>' .
           'Message: ' .$_REQUEST['message'] . ' <br/>' .
           'Requesting Information On: ' . $more_info . ' <br/>' .
           'Have a Question About: ' . $questions . ' <br/>';
if (@mail($to, $subject, $message, $headers)) {
    echo 'Your Message has been sent, Thank you!';
} else {
    echo 'Oops there was a problem and your message was not sent';
}
?>

希望这有帮助!