0

Can anyone help with a form problem that I just can't fathom? I have a group of checkboxes whose values I can't get to show up in the resulting mail.

This is the method I'm using to set the variables:

    $benefit01  = ($_POST['benefit_01']);
$benefit02  = ($_POST['benefit_02']);
$benefit03  = ($_POST['benefit_03']);
$benefit04  = ($_POST['benefit_04']);
$benefit05  = ($_POST['benefit_05']);
$benefit06  = ($_POST['benefit_06']);

$property   = addslashes($_POST['property']);
$owner      = addslashes($_POST['owner']);
$mainHeating    = addslashes($_POST['Heating_mainHeating_answer']);
$boiler_working = addslashes($_POST['boiler_working']);
$boiler_age = addslashes($_POST['boiler_age']);

$postcode   = addslashes($_POST['postcode']);
$address1   = addslashes($_POST['address1']);
$address2   = addslashes($_POST['address2']);
$address3   = addslashes($_POST['address3']);
$city       = addslashes($_POST['town']);
$county     = addslashes($_POST['county']);

$name       = addslashes($_POST['firstName']);
$surname    = addslashes($_POST['surname']);
$email      = addslashes($_POST['email']);
$phone      = addslashes($_POST['phonenum']);


$type   = addslashes($_POST['type']);
$time   =   $_POST['time'];
$src = $_POST['urlsrc'] ;

$benefits   = $benefit01." ".$benefit02." ".$benefit03." ".$benefit04." ".$benefit05." ".$benefit06;
$address = $address1." ".$address2;
$note = "Benefits: ".$benefits.". Property Type: ".$property.". Home Owner: ".$owner.". Type of Heating: ".$mainHeating.". Boiler Working: ".$boiler_working.". Boiler Age: ".$boiler_age.". Timescales: ".$time.". Address Line 3: ".$address3;

So the "benefits" are checkboxes. I had tried naming them all the same to create an array then imploding the values but that didn't work either. Ultimately this data is sent to a dbase which is why most of the info is dumped into a $note variable.

I don't know much about this and have taken this example from a working (albeit simplified) form. Any assistance appreciated.

** Small sample of the form added below:

    <input name="ctl00_chkBenefit" type="checkbox" id="benefit_01" class="checkbox-benefit" value="Child Tax Credit" />

Child Tax Credit (where the relevant income is £15,860 or less)

     <input name="ctl01_chkBenefit" type="checkbox" id="benefit_02" class="checkbox-benefit" value="State Pension Credit" />

State Pension Credit

dvmac01
  • 449
  • 1
  • 5
  • 13
  • Please, add here your HTML of form. – Legionar Nov 13 '13 at 12:11
  • FYI: [`addslashes` does not protect fully against SQL injection.](http://stackoverflow.com/questions/860954/examples-of-sql-injections-through-addslashes). (*I'm assuming that's why you're add slashing - if not - just ignore this comment*). – h2ooooooo Nov 13 '13 at 12:14
  • Thanks h2ooooooo, I'll investigate that more. – dvmac01 Nov 13 '13 at 12:54

4 Answers4

1

Is your checkbox's HTML like this?

<input type="checkbox" name="benefit_01" value="Benefit 01" /> Benefit 01

Then if that checkbox will be checked by user, $_POST['benefit_01'] will be the value of that checkbox - Benefit 01.

If it will be not checked, $_POST['benefit_01'] will be not set.

EDITED 1:

And that all you should have in <form>, with setted method and action:

<form action="index.php" method="post">
     <input name="ctl00_chkBenefit" type="checkbox" id="benefit_01" class="checkbox-benefit" value="Child Tax Credit" />
</form>

EDITED 2:

You have different name for that checkboxes. You used in your $_POST theirs id, not name.

Change your code to this:

$benefit01  = ($_POST['ctl00_chkBenefit']);
$benefit02  = ($_POST['ctl01_chkBenefit']);
$benefit03  = ($_POST['ctl02_chkBenefit']);
$benefit04  = ($_POST['ctl03_chkBenefit']);
$benefit05  = ($_POST['ctl04_chkBenefit']);
$benefit06  = ($_POST['ctl05_chkBenefit']);

Now it will work for 100% :)

Legionar
  • 7,472
  • 2
  • 41
  • 70
  • Thanks Legionar, even when the fields are checked nothing comes through. – dvmac01 Nov 13 '13 at 12:41
  • The method is Post and it's all wrapped in a form tag. There is a complete form with many other fields and ALL of the other values come through fine. Just not the benefits. Here is the url:http://www.dvmac.co.uk/ths/index.php?action=ecoProcess&src= – dvmac01 Nov 13 '13 at 12:51
  • You have different name - not `benefit_01`, but `ctl00_chkBenefit`. Thats the reason. See my edit... – Legionar Nov 13 '13 at 13:02
  • Thanks Legionar, I was just about to ask if that would make a difference. So the NAME is addressed and not the ID? – dvmac01 Nov 13 '13 at 13:18
  • You are welcome. Yes, in `$_POST` name tag is used. id tag is used f.e. for jQuery to access element. – Legionar Nov 13 '13 at 13:20
1

First of all,

<input type="checkbox" name="benefits[]" value="one">
<input type="checkbox" name="benefits[]" value="2">
<input type="checkbox" name="benefits[]" value="III">

would create an array $_POST['benefits'] in PHP.

Second, the variable will only contain the value if the checkbox is checked.

Alexander
  • 19,906
  • 19
  • 75
  • 162
  • I've tried using the same names to create an array but no values are output even when the boxes are checked. This is how I tried to display them: '$benefit = implode(', ', $_POST['benefit']);' – dvmac01 Nov 13 '13 at 12:45
  • Well, there is a difference between `$_POST['benefit']` and `$_POST['benefits']`- can you spot it? – Alexander Aug 24 '15 at 10:25
0

Input checkboxes aren't set in $_POST if not checked by the user. You can use empty() to determine if they're set and/or empty, and then set the value to the one you want to insert into the database.

mtaanquist
  • 506
  • 4
  • 16
-1
crate the input field name like this 

<input type="checkbox" name="c[]" value="1">
<input type="checkbox" name="c[]" value="2">
<input type="checkbox" name="c[]" value="3">
<input type="submit" >

And PHP you can write like this-

<?php 
$_POST['benefits'] 
?>
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
D-2020365
  • 82
  • 1
  • 11