4

would be grateful for some help on this:

I have a set of checkboxes where users are encouraged to select at least one option. I would like to store all of these options in a single variable using PHP and then in turn attach this variable on to the end of an email body to send to the site host using email. However, I can only seem to retrieve the last option that the user selects. I have tried using the array syntax (e.g. name="instrument[]") but I cannot seem to find a way to retrieve the values from this array. I have used the 'implode' method to store the post value but I keep on receiving an error message, saying "Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\academy\contact-process.php on line 8".

My code is below:

Contact.html

<tr>
    <td>
        <label id="checkbox">Piano</label>
        <input id="piano" type="checkbox" name="instrument[]" value="Piano" class="required" title="Please check at least one option.">
    </td>
    <td>
        <label id="checkbox">Flute</label>
        <input id="flute" type="checkbox" name="instrument[]" value="Flute">
    </td>
    <td>
        <label id="checkbox">Singing</label>
        <input id="singing" type="checkbox" name="instrument[]" value="Singing">
    </td>
</tr>


<tr>
    <td>
        <label id="checkbox">Violin</label>
        <input id="violin" type="checkbox" name="instrument[]" value="Violin">
    </td>

Contact-process.php:

<pre><?php


$title = $_POST["title"];
$forname = $_POST["firstname"];
$surname = $_POST["lastname"];
$phone = $_POST["phone"];
$instrument = implode(' ', $_POST["instrument"]);
$hear = $_POST["method"];       
$enrole = $_POST["child"];
$dob = $_POST["date"];
$message = $_POST["enquiry"];




$email_body = "";
$email_body = $email_body . "Title: " . $title . "\n";
$email_body = $email_body . "Forname: " . $forname . "\n";
$email_body = $email_body . "Surname: " . $surname . "\n";
$email_body = $email_body . "Telephone Number: " . $phone . "\n";
$email_body = $email_body . "Heard About You From: " . $hear . "\n";
$email_body = $email_body . "Interested In Learning: " . $instrument . "\n";
$email_body = $email_body . "Would Like To Enrole A(n): " . $enrole . "\n";
$email_body = $email_body . "Child's Date of Birth: " . $dob . "\n";
$email_body = $email_body . "Message: " . $message;

echo $email_body; 

?></pre>

At the moment this generates the error message as described above.

Anyone know a solution?

Thanks so much for any replies!!

Robert.

jcubic
  • 61,973
  • 54
  • 229
  • 402
Rob644
  • 53
  • 1
  • 4
  • 15
  • 1
    can you provide your HTML code in More details. – nana.chorage Jun 24 '13 at 11:48
  • Be sure to keep id's unique the label id, could be an problem in the future. – Jonathan Römer Jun 24 '13 at 11:53
  • 2
    Rob644, your code is ok (at least this part you provided for us) - however - before you implode() $instrument array, you have to check if it is existing array (if user don't choose one of options - nothing sent to server) - error is when there are no elements in array. So check if(isset($_POST['instrument'])) {implode....} P.S. Do that with the rest of vars too... – sinisake Jun 24 '13 at 12:08
  • Your code looks OK. Please see this thread: http://stackoverflow.com/questions/14026361/php-multiple-checkbox-array – Halil May 07 '14 at 12:35

5 Answers5

3

Below is your array input type now.

 <input id="singing" type="checkbox" name="instrument[]" value="Singing">

you have to handle it by looping it like this

 foreach($_POST['instrument'] as $instrument)
{
    echo $instrument.'<br>';
}
Muhammad
  • 3,169
  • 5
  • 41
  • 70
  • to avoid implode error: if($_POST['instrument']) $instrument = implode(' ', $_POST["instrument"]); – Muhammad Jun 24 '13 at 12:19
  • Hi @Nomi , thanks for your response. Can you tell me how to correctly incorporate this loop into a contact.php page (i.e. should I put it after the variable has been defined (`$instrument = $_POST["instrument"];`), after the 'email_body' section or before both? – Rob644 Jun 24 '13 at 15:15
  • Btw, I am now getting this error: "Notice: Undefined index: instrument" even though the checkboxes have the name of 'instrument'. – Rob644 Jun 24 '13 at 15:19
  • define $instrument = ''; on top of your page and let me know if you still getting error or your code is not working. – Muhammad Jun 25 '13 at 04:15
2

try this,

$instrument = (is_array($_POST["instrument"])) ? implode(' ', $_POST["instrument"]) : '';
Kautil
  • 1,321
  • 9
  • 13
  • 1
    Thanks, but this gives me the message: Notice: Undefined index: instrument in C:\xampp\htdocs\academy\contact-process.php on line 9 – Rob644 Jun 24 '13 at 14:05
0

Have you tried browsing the $intrument array like this?

$email_body .= "Interested In Learning: ";
foreach($_POST['instrument'] as $value){
    $email_body .= $value;
}
$email_body .= "\n";

By the way, I prefer using "$email_body .=" which is equivalent to "$email_body = $email_body ." (but shorter :P).

Uraza
  • 556
  • 3
  • 17
0

You can also use the json_encode method for this problem.

json_encode($_POST['instrument']);

It will convert array to json string which is {'piono','Flute','Singing','Violin'}

Next time at the time of displaying already selected ones you can use

json_decode({'piono','Flute','Singing','Violin'});

It will covert json string to array and this json string weight is also low. You can store this json string directly in DB.

Eswara Reddy
  • 1,617
  • 1
  • 18
  • 28
0

I had a similar problem, and i'm using this code, and it works:

$var = nl2br(implode(', ', $_POST['var']));

to output the value in the mail, i use:

$msg .= "<p><strong>Var:</strong> ".$var."</p>\r\n";

and in the form i use this:

<input type="checkbox" name="var[]" ... />

The only problem i'm having now is that if the user leave all the checkboxes unchecked, PHP sends the mail but returns a warning "implode(): Invalid arguments passed"

Jonotespere
  • 23
  • 1
  • 4