html - Checkbox Value in form coming over blank PHP

932

I am trying to check the checked values to come over in email but they are coming over blank. I am using the bootstrap validation code.

I tried to use each of these as well with no luck:

$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[]'];

Here is my HTML:

  <div >
     <label><h4>WHAT ACCOUNTING SERVICES/FUNCTIONS ARE NEEDED?</h4>Mark as many as necessary)</label>
     <div >
      <label><input type="checkbox" name="accounting_services[]" value="Accounts Payable - Entering bills"/>Accounts Payable - Entering bills</label>
     </div>
     <div >
      <label><input type="checkbox" name="Accounting_Services[]" value="Accounts Payable – Paying Bills w/ Approval"/>Accounts Payable – Paying Bills w/ Approval</label>
     </div>
     <div >
      <label><input type="checkbox" name="Accounting_Services[]" value="Accounts Receivable – Invoicing clients"/>Accounts Receivable – Invoicing clients</label>
     </div>
     <div >
      <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>

Here is the PHP code:

<?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 protected]'; 
$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;            
?>
52

Answer

Solution:

Change your code as follow.

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

People are also looking for solutions to the problem: how to convert p tag to line break in php fpdf

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.