【问题标题】:Retrieve Checkbox Array in PHP and Send in Email在 PHP 中检索复选框数组并通过电子邮件发送
【发布时间】:2012-08-14 05:42:49
【问题描述】:

我正在尝试将以下复选框提供给 PHP 电子邮件表单:

<div id="product_information_container">
    <h1 id="product_information_subtitle"><a data-tooltip="Click to hide/display the content below." href="#"><img class="information_icons" src="images/information_icon.png"></a>What can we help you with?</h1>
    <div class="product_information">
        <div class="product_information_containers" id="product_information_attic_barrier">
            <input id="product_information_checkbox_attic_barrier" name="product_information[]" type="checkbox" value="attic_barrier" />
            <span class="product_options">Attic Barrier</span>
        </div>
    </div>
    <div class="product_information">
        <div class="product_information_containers" id="product_information_doors">
            <input id="product_information_checkbox_doors" name="product_information[]" type="checkbox" value="entry_system_door" />
            <span class="product_options">Entry System Door</span>
        </div>
    </div>
    <div class="product_information">
        <div class="product_information_containers" id="product_information_eavestrough_cover">
            <input id="product_information_checkbox_eavestrough_cover" name="product_information[]" type="checkbox" value="eavestrough_cover" />
            <span class="product_options">Eavestrough Cover</span>
        </div>
    </div>
    <div class="product_information">
        <div class="product_information_containers" id="product_information_windows">
            <input id="product_information_checkbox_windows" name="product_information[]" type="checkbox" value="windows" />
            <span class="product_options">Windows</span>
        </div>
    </div>
    <div class="product_information" id="window_options">
        <div id="windows_options_one">
            <div class="product_information_windows_containers" id="product_information_windows_baybow">
                <input id="windows_baybow_checkbox" name="product_information[]" type="checkbox" value="windows" />
                <span class="product_options">Bay/Bow</span>
            </div>
            <div class="product_information_windows_containers" id="product_information_windows_casement">
                <input id="windows_casement_checkbox" name="product_information[]" type="checkbox" value="windows" />
                <span class="product_options">Casement</span>
            </div>
        </div>
        <div id="windows_options_two">
            <div class="product_information_windows_containers" id="product_information_windows_doublehung">
                <input id="windows_doublehung_checkbox" name="product_information[]" type="checkbox" value="windows" />
                <span class="product_options">Double-Hung</span>
            </div>
            <div class="product_information_windows_containers" id="product_information_windows_sliding">
                <input id="windows_sliding_checkbox" name="product_information[]" type="checkbox" value="windows" />
                <span class="product_options">Sliding</span>
            </div>
        </div>
    </div>
</div>

然后我打算捕获输入的信息,并通知哪些信息是相关的。本质上,在“product_information”中选择的所有产品都应该通过,如果选中的话。但是,使用我当前的代码,我只收到复选框组中选择的最后一个值。

这是我的 PHP 代码:

<?php  
if(isset($_POST)){
    $ip_address = $_SERVER['REMOTE_ADDR'];
    $date = date('d/m/Y');
    $time = date('H:i:s');

    $first_name = $_POST['first_name'];
    $spouse_name = $_POST['spouse_name'];
    $last_name = $_POST['last_name'];
    $street_number = $_POST['street_number'];
    $street_name = $_POST['street_name'];
    $street_type = $_POST['street_type'];
    $city = $_POST['city'];
    $postal_code = $_POST['postal_code'];
    $phone_number = $_POST['phone_number'];
    $alternate_phone_number = $_POST['alternate_phone_number'];
    $email_address = $_POST['email_address'];
    $contact_time = $_POST['contact_time'];
    $product_information = $_POST['product_information'];

    $headers = "From: from_email@lol.com" . "\r\n";  
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  

    $emailbody = "<p>You have received a message!</p> 
                  <strong>Name: </strong>{$first_name} {$last_name}, <strong>Spouse's Name: </strong>{$spouse_name}<br />
                  <strong>Address: </strong>{$street_number} {$street_name} {$street_type}, {$city}, ON {$postal_code}<br />
                  <strong>Phone Number: </strong> {$phone_number}, <strong>Alt. Phone Number: </strong> {$alternate_phone_number}<br />
                  <strong>Email Address: </strong> {$email_address}</p>
                  <p>Please contact {$first_name} during the {$contact_time} about {$product_information}.<br
                  <p>This message was sent from the IP Address: {$ip_address} on {$date} at {$time}.</p>";  

    mail('to_email@lol.com','Title',$emailbody,$headers);
}
?>

【问题讨论】:

  • $_POST['product_information'] 的值应该是一个数组。我很难相信您的输出是复选框组中选择的最后一个值,而不是Array
  • 所有复选框值都具有相同的 windows 值。这是怎么回事?
  • 这是我选择的内容:imageshack.us/f/26/beforese.jpg 这是我得到的内容:imageshack.us/f/855/resultsv.jpg

标签: php arrays forms post checkbox


【解决方案1】:

$_POST['product_information'] 是一个数组,也许你会发现有用的implode 函数可以用分隔符连接所有值:

product_information = implode($_POST['product_information'], ',');

【讨论】:

    【解决方案2】:

    每个复选框都需要自己的值,但在 WindowsBay/BowCasementDouble-HungSliding 上,它们都是 value="windows",所以如果所有这些都被选中,你会得到数组

    Array ( [product_information] => Array ( [0] => windows
                                             [1] => windows
                                             [2] => windows
                                             [3] => windows
                                             [4] => windows ) )`
    

    改成类似-

    <div class="product_information">
        <div class="product_information_containers" id="product_information_windows">
            <input id="product_information_checkbox_windows" name="product_information[]" type="checkbox" value="windows" />
            <span class="product_options">Windows</span>
        </div>
    </div>
    <div class="product_information" id="window_options">
        <div id="windows_options_one">
            <div class="product_information_windows_containers" id="product_information_windows_baybow">
                <input id="windows_baybow_checkbox" name="product_information[]" type="checkbox" value="baybow" />
                <span class="product_options">Bay/Bow</span>
            </div>
            <div class="product_information_windows_containers" id="product_information_windows_casement">
                <input id="windows_casement_checkbox" name="product_information[]" type="checkbox" value="casement" />
                <span class="product_options">Casement</span>
            </div>
        </div>
        <div id="windows_options_two">
            <div class="product_information_windows_containers" id="product_information_windows_doublehung">
                <input id="windows_doublehung_checkbox" name="product_information[]" type="checkbox" value="doublehung" />
                <span class="product_options">Double-Hung</span>
            </div>
            <div class="product_information_windows_containers" id="product_information_windows_sliding">
                <input id="windows_sliding_checkbox" name="product_information[]" type="checkbox" value="sliding" />
                <span class="product_options">Sliding</span>
            </div>
        </div>
    </div>
    

    现在你将拥有一个合适的数组

    Array ( [product_information] => Array ( [0] => windows 
                                             [1] => baybow 
                                             [2] => casement 
                                             [3] => doublehung 
                                             [4] => sliding ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-08
      • 1970-01-01
      • 2019-01-13
      • 2012-01-19
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 2012-06-29
      相关资源
      最近更新 更多