提交到电子邮件时,PHP表单带有复选框和下拉列表问题

时间:2012-09-06 15:55:55

标签: php forms form-submit checkbox

好吧,我对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']);

    }


}

?>

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

1 个答案:

答案 0 :(得分:2)

我猜这一行:

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

刚刚返回Array。

您需要遍历此数组以获取已检查的数组:

$timeAv = array();

foreach($timez as $time) {

    if(!empty($time)) {

        $timeAv[] = $time;

    }
}

$timeStr = implode(', ', $timeAv);

这应该构建一个经过检查的数组,然后将它们添加到字符串中。我希望我理解正确 - 否则就会浪费几行代码!

由于