PHP表单在提交到电子邮件时出现复选框和下拉问题


PHP Form with Checkboxes and Dropdown problems when submitting to email

好吧,我对php或表单不是很好,但我已经做了很多挖掘,没有找到任何解决方案来解决如何工作。我有一个表格,有两组复选框和一个下拉列表,在标准名称、电子邮件等字段中。我的问题是让复选框的值显示在表单提交时收到的电子邮件中。这是表单代码:

    <form method="post" action="appointment-process.php">
<fieldset>
    <label for="name">Name<em class="warning">*</em></label>
    <input id="name" name="name" type="text" class="span6" autofocus required placeholder="Raider Red">
    <span class="help-block">Please enter your FIRST and LAST name.</span>
    <label for="email">Email Address<em class="warning">*</em></label>
    <input id="email" name="email" type="email" class="span6" placeholder="raider.red@ttu.edu" required>
    <label for="phone">Phone Number<em class="warning">*</em></label>
    <input id="phone" name="phone" type="tel" class="span6" placeholder="8067421234" required>
    <label for="legal">Legal Issues<em class="warning">*</em></label>
    <input id="legal" name="legal" type="text" class="span6" placeholder="Landlord Problems" required>  
    <label for="classification">Classification</label>
    <div class="controls">                              
        <select id="classification" name="classification">
            <option>Freshman</option>
            <option>Sophomore</option>
            <option>Junior</option>
            <option>Senior</option>
            <option>Other</option>
        </select>                                       
    </div>
    <div class="control-group">
        <label for="">Preferred Appointment Times</label>
        <span class="help-block">Please check all of the days and times that work with your schedule</span>
        <div class="controls row">
                <div class="span3">
                    <label class="checkbox" for="timez">
                        <input type="checkbox" name="timez[]" value="9am">9:00 am 
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="timez[]" value="10am">10:00 am
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="timez[]" value="11am">11:00 am
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="timez[]" value="1:30pm">1:30 pm
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="timez[]" value="2:30pm">2:30 pm
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="timez[]" value="3:30pm">3:30 pm
                    </label>
                </div>                                              
                <div class="span3">
                    <label class="checkbox" for="dayz">
                        <input type="checkbox" name="dayz[]" value="Monday">Monday
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="dayz[]" value="Tuesday">Tuesday
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="dayz[]" value="Wednesday">Wednesday
                    </label>
                    <label class="checkbox">
                        <input type="checkbox" name="dayz[]" value="Thursday">Thursday
                    </label>
                </div>
        </div>
    </div>
    <button type="submit" class="btn">Submit</button>
</fieldset> 
</form>

这是我的php代码:

<?php  
if( isset($_POST) ){  
    //form validation vars  
    $formok = true;  
    $errors = array();  
    //submission data  
    $ipaddress = $_SERVER['REMOTE_ADDR'];  
    $date = date('d/m/Y');  
    $time = date('H:i:s');
    //form data
    $name = $_POST['name'] ;
    $email = $_POST['email'] ;
    $phone = $_POST['phone'] ;
    $legal = $_POST['legal'] ;
    $timez = $_POST['timez'] ;
    //$dayz = $_POST['dayz'];
    //form validation to go here....
    //validate name is not empty
    if(empty($name)){
        $formok = false;
        $errors[] = "You have not entered a name";
    }
    //validate email address is not empty
    if(empty($email)){
        $formok = false;
        $errors[] = "You have not entered an email address";
    //validate email address is valid
    }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
        $formok = false;
        $errors[] = "You have not entered a valid email address";
    }
    //validate phone number is not empty
    if(empty($phone)){
        $formok = false;
        $errors[] = "You have not entered a phone number";
    }
    //validate legal issue is not empty
    if(empty($legal)){
        $formok = false;
        $errors[] = "You have not entered a legal issue";
    }
    if(isset($_POST['dayz'])){
        $message .= implode(', ', $_POST['dayz']);
    }
    //send email if all is ok
    if($formok){
        $headers = "From: email@email.com" . "'r'n";
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "'r'n";
        $emailbody = "<p>You have recieved a new Appointment Request.</p>
                      <p><strong>Name: </strong> {$name} </p>
                      <p><strong>Email Address: </strong> {$email} </p>
                      <p><strong>Phone Number: </strong> {$phone} </p>
                      <p><strong>Legal Issue: </strong> {$legal} </p>
                      <p><strong>Classification: </strong> {} </p>
                      <p><strong>Preferred Appointment Day(s): </strong> {$dayz} </p>
                      <p><strong>Preferred APpointment Time(s): </strong> {$timez} </p>                   
                      <p>This message was sent on {$date} at {$time}</p>";
        imap_mail("email@email.com","Title",$emailbody,$headers);
    }
    //what we need to return back to our form
    $returndata = array(
        'posted_form_data' => array(
            'name' => $name,
            'email' => $email,
            'timez' => $timez,
            'dayz' => $dayz
        ),
        'form_ok' => $formok,
        'errors' => $errors
    );

    //if this is not an ajax request
    if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
        //set session variables
        session_start();
        $_SESSION['cf_returndata'] = $returndata;
        //redirect back to form
        header('location: ' . $_SERVER['HTTP_REFERER']);
    }

}
?>

任何帮助都将不胜感激。这是我第一次在这里发帖,所以如果我没有遵循正确的协议,请告诉我,我会纠正的。谢谢

我猜这一行:

<p><strong>Preferred APpointment Time(s): </strong> {$timez} </p>   

正在返回Array。

您需要遍历此数组以获得选中的数组:

$timeAv = array();
foreach($timez as $time) {
    if(!empty($time)) {
        $timeAv[] = $time;
    }
}
$timeStr = implode(', ', $timeAv);

这应该建立一个检查的时间数组,然后将它们添加到一个字符串中。我希望我能正确理解——否则就是浪费了几行代码!

感谢