Forum Moderators: coopster
into my html page....
<div>
<label for="netsecurity">My Security Includes<br><font size=1><i>Check all that apply...</I></font></label>
<input type="checkbox" name="netsecurity[]" value="Firewall Appliance" style="height:15;width:15px;border:0"/>Firewall Appliance<br>
<input type="checkbox" name="netsecurity[]" value="Firewall Suite (Free Software)" style="height:15;width:15px;border:0"/>Firewall Suite (Free Software)<br>
<input type="checkbox" name="netsecurity[]" value="Firewall Suite" style="height:15;width:15px;border:0"/>Firewall Suite<br>
<input type="checkbox" name="netsecurity[]" value="Antivirus Protection (Free Software)" style="height:15;width:15px;border:0"/>Antivirus Protection (Free Software)<br>
<input type="checkbox" name="netsecurity[]" value="Antivirus Protection" style="height:15;width:15px;border:0" />Antivirus Protection<br>
<input type="checkbox" name="netsecurity[]" value="Backup Procedure" style="height:15;width:15px;border:0"/>Backup Procedure<br>
<input type="checkbox" name="netsecurity[]" value="Network Documentation" style="height:15;width:15px;border:0"/>Network Documentation
</div>
into my php file...
function ldsec($ckd)
{
$items = "";
$item = "";
print_r($ckd);
for ($i=0; $i<count($ckd); $i++)
{
if(isset($ckd[$i]))
{
$item = $ckd[$i];
if($items == "")
{
$items = $item;
}
else
{
$itmes = $items.", ".$item;
}
}
}
return $items;
}
$secfeatures = ldsec($netsecurity);
the problem is that I'm only getting the first item checked, not all of them...
What am I missing here?
Thank your very much for answering!
$secfeatures = ldsec($netsecurity);
How are you setting $netsecurity? Only if register_globals is set on your server will $netsecurity be set automatically (but this is unwise), otherwise the more usual way would be to check $_POST['netsecurity'] (if you are POSTing your form data)?
Also remember that checkbox elements are only passed back to PHP if they are set. If they are not set, then there will be no element in the array for them. If no checkboxes are checked then an empty array will be returned (or is the array even set?).
What does
print_r($ckd)return?
Also, I would be more inclined to use a foreach()
[uk2.php.net] construct to step over your array, instead of
for()
inside of the PHP code, after the changes recommended:
function ldsec($ckd)
{
$items = "";
$item = "";
if(isset($_POST[$ckd]))
{
foreach($_POST[$ckd] as $value)
{
$item = $POST[$ckd];
if($items == "")
{
$items = $item;
}
else
{
$itmes = $items.", ".$item;
}
}
}
return $items;
}
$secfeatures = ldsec($netsecurity); (here I called the function, passing the field as an argument - it is supposed to go as an array)
$frm = "Visitor's information:
Name: $name
Email: $email
- Interested in service for: $tpsvr
- Network Server: $netsvr
- Network Type: $typenet
- Network Size: $netsize
- Computer type: $comptype
- Network Security features: $secfeatures (here is where I want every checked box to be shown)
Message:
$message";
mail($myemail, $subject, $frm);
errors on the php log
PHP Warning: Illegal offset type in /newsite/scripts/validation.php
Thank youy very much for the replay, even on mother's day! you guys rock!
You're welcome, but it wasn't actually mother's day over here - we 'celebrated' that one back in March! It was 'just another day' here, albeit a rather pleasant sunny one. :)
As regards your code... I suppose I was intending that $_POST['netsecurity'] was passed to your ldesc() function instead of $netsecurity (which might not be set). However, this value does not necessarily need to be passed to the function. $_POST['netsecurity'] could be referred to directly inside your function and not pass anything to it - which is I think how bkeep was intending. Either way has its merits, but I think this may have confused you.
In keeping with your original code and passing an argument to the function (so you have a generic function) we have something like...
function ldsec($ckd) {
if (!is_array($ckd)) {
return '[Nothing Selected]';
}
$items = '';
foreach($ckd as $value) {
if (!empty($items)) {
$items .= ', ';
}
$items .= $value;
}
return $items;
}
$secfeatures = '[Nothing Selected]';
if (!empty($_POST['netsecurity'])) {
$secfeatures = ldsec($_POST['netsecurity']);
} $value is your $item variable (so I've removed $item). And 'simplified' it a bit. This could be improved however. Instead of passing the entire $_POST['netsecurity'] array to your function, you could perhaps just pass the element name (ie. 'netsecurity') instead and avoid having to check for the empty state (ie. '[Nothing Selected]') twice.
Any queries with the code just say.
This could be improved however. Instead of passing the entire $_POST['netsecurity'] array to your function, you could perhaps just pass the element name (ie. 'netsecurity') instead and avoid having to check for the empty state (ie. '[Nothing Selected]') twice.
This is perhaps better explained with an example...
function ldsec($name) {
$ckd = isset($_POST[$name]) ? $_POST[$name] : null;
if (!is_array($ckd)) {
return '[Nothing Selected]';
}
$items = '';
foreach($ckd as $value) {
if (!empty($items)) {
$items .= ', ';
}
$items .= $value;
}
return $items;
}
$secfeatures = ldsec('netsecurity');