1

わかりました、私はphpやフォームがあまり得意ではありませんが、多くの掘り下げを行い、機能させる方法を理解できる解決策を思いつきませんでした. 標準名、電子メールなどのフィールドの中に、2セットのチェックボックスとドロップダウンがあるフォームがあります。私の問題は、フォームの送信時に受信した電子メールに表示されるチェックボックスの値を取得することです。フォームコードは次のとおりです。

    <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']);

    }


}

?>

どんな助けでも本当に感謝しています。ここに投稿するのはこれが初めてなので、正しいプロトコルに従わなかった場合はお知らせください。修正します. ありがとう!

4

1 に答える 1

2

私はこの行を推測しています:

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

配列を返すだけです。

チェックされた配列を取得するには、この配列を反復処理する必要があります。

$timeAv = array();

foreach($timez as $time) {

    if(!empty($time)) {

        $timeAv[] = $time;

    }
}

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

これにより、チェックされた時間の配列が構築され、それらが文字列に追加されます。私が正しく理解したことを願っています-そうでなければ、それは数行のコードが無駄になります!

ありがとう

于 2012-09-06T16:02:42.177 に答える