Member Avatar for feddie1984

Hi All,

I have the following code:

$requester_email = $_POST['requester_email'];
$requester_name = $_POST['requester_name'];
$todays_date = $_POST['todays_date'];
$Start_Date = $_POST['Start_Date'];
$End_Date = $_POST['End_Date'];
$Department = $_POST['Department'];
$No_Engineers_Required = $_POST['No_Engineers_Required'];
$Prefered_Engineers = $_POST['Prefered_Engineers'];
$Customer_Name = $_POST['Customer_Name'];
$Location = $_POST['Location'];
$Type_Of_Job = $_POST['Type_Of_Job'];
$Job_Ref_No = $_POST['Job_Ref_No'];
$Additional_info = $_POST['Additional_info'];

The help I need is the information for Prefered_Engineers comes from a collection of checkboxes.

This code is here:

<td>
<font color="#000000" size="2" face="Arial">
<input name="Prefered_Engineers[]" type="checkbox" value="GT" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="CT" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="GB" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="GK" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="MH" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="DR" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="JM" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="LL" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="CJ" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="NW" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="HG" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="DH" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="JW" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="JB" class="fieldstyle">
</font>
</td>

The value passed is 'Array'.

Can anyone help so that all of the selected checkbox values are passed.

Regards,

T

Recommended Answers

All 15 Replies

Remove [] from the name of the field, PHP sees that and tries to turn it into an array.

Hi All,

I have the following code:

$requester_email = $_POST['requester_email'];
$requester_name = $_POST['requester_name'];
$todays_date = $_POST['todays_date'];
$Start_Date = $_POST['Start_Date'];
$End_Date = $_POST['End_Date'];
$Department = $_POST['Department'];
$No_Engineers_Required = $_POST['No_Engineers_Required'];
$Prefered_Engineers = $_POST['Prefered_Engineers'];
$Customer_Name = $_POST['Customer_Name'];
$Location = $_POST['Location'];
$Type_Of_Job = $_POST['Type_Of_Job'];
$Job_Ref_No = $_POST['Job_Ref_No'];
$Additional_info = $_POST['Additional_info'];

The help I need is the information for Prefered_Engineers comes from a collection of checkboxes.

This code is here:

<td>
<font color="#000000" size="2" face="Arial">
<input name="Prefered_Engineers[]" type="checkbox" value="GT" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="CT" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="GB" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="GK" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="MH" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="DR" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="JM" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="LL" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="CJ" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="NW" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="HG" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="DH" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="JW" class="fieldstyle">
<br>
<input name="Prefered_Engineers[]" type="checkbox" value="JB" class="fieldstyle">
</font>
</td>

The value passed is 'Array'.

Can anyone help so that all of the selected checkbox values are passed.

Regards,

T

As much as I try and aviod overposting more experienced and respected posters, if you remove the [] that will only let you select 1 of the checkboxes and not multiple. If you want multiple You need to loop through them. Try

<?php
  $engineers = $_POST['Prefered_Engineers'];
  foreach($engineers as $engineer) {
     echo $engineer;
  }
?>

Hope that helps

commented: good spot +6

As much as I try and aviod overposting more experienced and respected posters, if you remove the [] that will only let you select 1 of the checkboxes and not multiple. If you want multiple You need to loop through them. Try

<?php
  $engineers = $_POST['Prefered_Engineers'];
  foreach($engineers as $engineer) {
     echo $engineer;
  }
?>

Hope that helps

What he said, it's been a long day :)

What he said, it's been a long day :)

It happens to the best of us ;)

Member Avatar for feddie1984

Thank you both for your replies. Is there any way to pass the looped result into the variable $Prefered_Engineers? The out put I get now is the selected checkboxes printed on the screen but only the last selected checkbox being posted to Prefered_Engineers.

$requester_email = $_POST['requester_email'];
$requester_name = $_POST['requester_name'];
$todays_date = $_POST['todays_date'];
$Start_Date = $_POST['Start_Date'];
$End_Date = $_POST['End_Date'];
$Department = $_POST['Department'];
$No_Engineers_Required = $_POST['No_Engineers_Required'];
$Prefered_Engineers = $_POST['Prefered_Engineers'];
foreach($Prefered_Engineers as $Prefered_Engineers) {
echo $Prefered_Engineers;
}
$Customer_Name = $_POST['Customer_Name'];
$Location = $_POST['Location'];
$Type_Of_Job = $_POST['Type_Of_Job'];
$Job_Ref_No = $_POST['Job_Ref_No'];
$Additional_info = $_POST['Additional_info'];

What I am doing is displaying the information on the webpage, sending and e-mail and inserting the info into a database. Everything else works apart from the checkboxes. I got it to work like this:

$Prefered_Engineers = serialize($_POST['Prefered_Engineers']);

but the output is:

Prefered Engineers: a:3:{i:0;s:2:"GK";i:1;s:2:"DR";i:2;s:2:"LL";}

Is there a way to display and store it as GK, DR, LL?

Thank you both for your replies. Is there any way to pass the looped result into the variable $Prefered_Engineers? The out put I get now is the selected checkboxes printed on the screen but only the last selected checkbox being posted to Prefered_Engineers.

$requester_email = $_POST['requester_email'];
$requester_name = $_POST['requester_name'];
$todays_date = $_POST['todays_date'];
$Start_Date = $_POST['Start_Date'];
$End_Date = $_POST['End_Date'];
$Department = $_POST['Department'];
$No_Engineers_Required = $_POST['No_Engineers_Required'];
$Prefered_Engineers = $_POST['Prefered_Engineers'];
foreach($Prefered_Engineers as $Prefered_Engineers) {
echo $Prefered_Engineers;
}
$Customer_Name = $_POST['Customer_Name'];
$Location = $_POST['Location'];
$Type_Of_Job = $_POST['Type_Of_Job'];
$Job_Ref_No = $_POST['Job_Ref_No'];
$Additional_info = $_POST['Additional_info'];

What I am doing is displaying the information on the webpage, sending and e-mail and inserting the info into a database. Everything else works apart from the checkboxes. I got it to work like this:

$Prefered_Engineers = serialize($_POST['Prefered_Engineers']);

but the output is:

Prefered Engineers: a:3:{i:0;s:2:"GK";i:1;s:2:"DR";i:2;s:2:"LL";}

Is there a way to display and store it as GK, DR, LL?

apologies the function was a demonstration of how to access the the values in the array. you can use the same function to perform other actions, such as send the values to the data base. It will depend on your database structure as to how you would insert the values.

I am not too sure what you mean about the GK, DR, LL

Member Avatar for feddie1984

Sorry that was an eample of how I would like the information stored in the field. This is my script for php:

<?php


include("socomecdbinfo.inc.php");


$requester_email = $_POST['requester_email'];
$requester_name = $_POST['requester_name'];
$todays_date = $_POST['todays_date'];
$Start_Date = $_POST['Start_Date'];
$End_Date = $_POST['End_Date'];
$Department = $_POST['Department'];
$No_Engineers_Required = $_POST['No_Engineers_Required'];
$Prefered_Engineers = serialize($_POST['Prefered_Engineers']);
$Customer_Name = $_POST['Customer_Name'];
$Location = $_POST['Location'];
$Type_Of_Job = $_POST['Type_Of_Job'];
$Job_Ref_No = $_POST['Job_Ref_No'];
$Additional_info = $_POST['Additional_info'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO DateRequest VALUES ('','$requester_email','$requester_name','$todays_date','$Start_Date','$End_Date','$Department','$No_Engineers_Required','$Prefered_Engineers','$Customer_Name','$Location','$Type_Of_Job','$Job_Ref_No','$Additional_info')";
mysql_query($query);
mysql_close();

$message = " $todays_date \n
Requester: $requester_name \n
Start Date: $Start_Date \n
End Date: $End_Date \n
No Engineers Reqired: $No_Engineers_Required \n
Prefered Engineers: $Prefered_Engineers \n
Customer Name: $Customer_Name \n
Location: $Location \n
Type of Job: $Type_Of_Job \n
Job Ref No: $Job_Ref_No \n
Additional Info: $Additional_info \n
";

$from = "From: $requester_email\r\n";

mail("test@btinternet.com", "Date Request", $message, $from);

?>

<p align="center">
Date: <?php echo $todays_date ?>
<br />
Thank You : <?php echo $requester_name ?>
<br />
Message:<br />
<?php echo $message ?>

So instead of Prefered_Engineers = a:3:{i:0;s:2:"GK";i:1;s:2:"DR";i:2;s:2:"LL";}
I would like it to equal: GK, DR, LL

Sorry that was an eample of how I would like the information stored in the field. This is my script for php:

<?php


include("socomecdbinfo.inc.php");


$requester_email = $_POST['requester_email'];
$requester_name = $_POST['requester_name'];
$todays_date = $_POST['todays_date'];
$Start_Date = $_POST['Start_Date'];
$End_Date = $_POST['End_Date'];
$Department = $_POST['Department'];
$No_Engineers_Required = $_POST['No_Engineers_Required'];
$Prefered_Engineers = serialize($_POST['Prefered_Engineers']);
$Customer_Name = $_POST['Customer_Name'];
$Location = $_POST['Location'];
$Type_Of_Job = $_POST['Type_Of_Job'];
$Job_Ref_No = $_POST['Job_Ref_No'];
$Additional_info = $_POST['Additional_info'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO DateRequest VALUES ('','$requester_email','$requester_name','$todays_date','$Start_Date','$End_Date','$Department','$No_Engineers_Required','$Prefered_Engineers','$Customer_Name','$Location','$Type_Of_Job','$Job_Ref_No','$Additional_info')";
mysql_query($query);
mysql_close();

$message = " $todays_date \n
Requester: $requester_name \n
Start Date: $Start_Date \n
End Date: $End_Date \n
No Engineers Reqired: $No_Engineers_Required \n
Prefered Engineers: $Prefered_Engineers \n
Customer Name: $Customer_Name \n
Location: $Location \n
Type of Job: $Type_Of_Job \n
Job Ref No: $Job_Ref_No \n
Additional Info: $Additional_info \n
";

$from = "From: $requester_email\r\n";

mail("test@btinternet.com", "Date Request", $message, $from);

?>

<p align="center">
Date: <?php echo $todays_date ?>
<br />
Thank You : <?php echo $requester_name ?>
<br />
Message:<br />
<?php echo $message ?>

So instead of Prefered_Engineers = a:3:{i:0;s:2:"GK";i:1;s:2:"DR";i:2;s:2:"LL";}
I would like it to equal: GK, DR, LL

Oh i see...

<?php
   $string = '';
   $engineers = $_POST['Prefered_Engineers'];
   foreach($engineers as $engineer) {
      $string .= "{$string}, {$engineer} ";  
   }
?>

That will put all the engineers in to 1 string seperated by commas

you could also make a function out of it

<?php
   function arrayToString($array) {
      $string = '';
      foreach($array as $value) {
         $string .= "{$string}, {$value} ";
      }
      return $string;
   }

   echo arrayToString($_POST['Prefered_Engineers']);
?>

you could also make a function out of it

<?php
   function arrayToString($array) {
      $string = '';
      foreach($array as $value) {
         $string .= "{$string}, {$value} ";
      }
      return $string;
   }

   echo arrayToString($_POST['Prefered_Engineers']);
?>

Or simply,

$string = implode(",",$_POST['Prefered_Engineers']);
commented: Turned many lines of ordinary code into 1 line of magic +1
Member Avatar for feddie1984

Thanks for all your replies, I will test them when I get home and then mark as solved.

Member Avatar for feddie1984

Could I change $string to $Prefered_Engineers?

Like This:

<?php  


include("socomecdbinfo.inc.php");  


$requester_email = $_POST['requester_email'];
$requester_name = $_POST['requester_name'];
$todays_date = $_POST['todays_date'];
$Start_Date = $_POST['Start_Date'];
$End_Date = $_POST['End_Date'];
$Department = $_POST['Department'];
$No_Engineers_Required = $_POST['No_Engineers_Required'];
$Prefered_Engineers = implode(",",$_POST['Prefered_Engineers']);
$Customer_Name = $_POST['Customer_Name'];
$Location = $_POST['Location'];
$Type_Of_Job = $_POST['Type_Of_Job'];
$Job_Ref_No = $_POST['Job_Ref_No'];
$Additional_info = $_POST['Additional_info'];  


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO DateRequest VALUES ('','$requester_email','$requester_name','$todays_date','$Start_Date','$End_Date','$Department','$No_Engineers_Required','$Prefered_Engineers','$Customer_Name','$Location','$Type_Of_Job','$Job_Ref_No','$Additional_info')";
mysql_query($query);
mysql_close();

$message = " $todays_date \n
Requester: $requester_name \n
Start Date: $Start_Date \n
End Date: $End_Date \n
No Engineers Reqired: $No_Engineers_Required \n
Prefered Engineers: $Prefered_Engineers \n
Customer Name: $Customer_Name \n
Location: $Location \n
Type of Job: $Type_Of_Job \n
Job Ref No: $Job_Ref_No \n
Additional Info: $Additional_info \n";

$from = "From: $requester_email\r\n";

mail("test@btinternet.com", "Date Request", $message, $from);

?>

<p align="center">Date: <?php echo $todays_date ?>
<br />
Thank You : <?php echo $requester_name ?>
<br />
Message:<br />
<?php echo $message ?>

Yeah, ofcourse. :) $string is just a variable name. You can give any name to a variable.
See Php naming convention here .

Or simply,

$string = implode(",",$_POST['Prefered_Engineers']);

Haha... I should have known that: :$

Member Avatar for feddie1984

All sotrted and working just great. Thanks everyone for your help!

T

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.