【问题标题】:Checkbox Value in form coming over blank PHP表单中的复选框值来自空白 PHP
【发布时间】:2015-03-13 08:51:27
【问题描述】:

我正在尝试检查检查的值以通过电子邮件发送,但它们是空白的。我正在使用引导验证代码。

我也尝试使用其中的每一个,但没有成功:

$accounting_services = !empty($_POST['accounting_services[]']) ? array() : $_POST['accounting_services[]']; 

$accounting_services = !empty($_POST['accounting_services[]']) ? implode(' | ', $_POST['accounting_services[]']) 

$accounting_services = nl2br(implode(',',$_POST['accounting_services[]'];

这是我的 HTML:

    <div class="form-group">
         <label><h4>WHAT ACCOUNTING SERVICES/FUNCTIONS ARE NEEDED?</h4>Mark as many as necessary)</label>
         <div class="checkbox">
            <label><input type="checkbox" name="accounting_services[]" value="Accounts Payable - Entering bills"/>Accounts Payable - Entering bills</label>
         </div>
         <div class="checkbox">
            <label><input type="checkbox" name="Accounting_Services[]" value="Accounts Payable – Paying Bills w/ Approval"/>Accounts Payable – Paying Bills w/ Approval</label>
         </div>
         <div class="checkbox">
            <label><input type="checkbox" name="Accounting_Services[]" value="Accounts Receivable – Invoicing clients"/>Accounts Receivable – Invoicing clients</label>
         </div>
         <div class="checkbox">
            <label><input type="checkbox" name="Accounting_Services[]" value="Accounts Receivable – Entering receive payments and deposits"/>Accounts Receivable – Entering receive payments and deposits</label>
         </div>
</div>

这是 PHP 代码:

<?php
// check if fields passed are empty
if(empty($_POST['name'])        ||
   empty($_POST['businessname']) ||

   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }
    if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['accounting_services'])){
foreach($_POST['accounting_services'] as $accountingselected){
echo $accountingselected."</br>";
}
}
}
$name = $_POST['name'];
$businessname = $_POST['businessname'];

$to = 'email@email.com'; 
$email_subject = "Contact form submitted by:  $name from $businessname";
$email_body = "You have received a new message. \n\n".
                  " Here are the details:\n \n ".
                  "Name: $name \n ".
                  "Business Name: $businessname \n ".
                  "Accounting Services: $accountingselected \n".;
$headers = "From: $email_address\n";
$headers .= "Reply-To: $email_address"; 
mail($to,$email_subject,$email_body,$headers);
return true;            
?>

【问题讨论】:

  • [] 实际上在$_POST 中生成了一个数组值,因此您不要在键中包含[]。您应该访问!empty($_POST['Accounting_Services']),而不是!empty($_POST['accounting_services[]'])。要查看其内容和结构,请查看print_r($_POST); 的输出。注意数组键也区分大小写,并且您的第一个复选框的大小写与其他复选框不同。这是我立即看到的。解决这些问题后,可能还会出现其他问题。
  • 如果未选中复选框,则不会发送值。

标签: php html email checkbox


【解决方案1】:

如下更改您的代码。

$accounting_services = isset($_POST['accounting_services']) ?$_POST['accounting_services'] : ""; 

【讨论】:

  • 我尝试了您的代码,但它仍然无法正常工作,但我想我可能缺少一些 JavaScript。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
相关资源
最近更新 更多