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.

4
  • 1
    can you provide your HTML code in More details. Commented Jun 24, 2013 at 11:48
  • Be sure to keep id's unique the label id, could be an problem in the future. Commented Jun 24, 2013 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... Commented Jun 24, 2013 at 12:08
  • Your code looks OK. Please see this thread: stackoverflow.com/questions/14026361/… Commented May 7, 2014 at 12:35

5 Answers 5

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>';
}
Sign up to request clarification or add additional context in comments.

4 Comments

to avoid implode error: if($_POST['instrument']) $instrument = implode(' ', $_POST["instrument"]);
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?
Btw, I am now getting this error: "Notice: Undefined index: instrument" even though the checkboxes have the name of 'instrument'.
define $instrument = ''; on top of your page and let me know if you still getting error or your code is not working.
2

try this,

$instrument = (is_array($_POST["instrument"])) ? implode(' ', $_POST["instrument"]) : '';

1 Comment

Thanks, but this gives me the message: Notice: Undefined index: instrument in C:\xampp\htdocs\academy\contact-process.php on line 9
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).

Comments

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"

Comments

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.

1 Comment

Can you develop your answer a bit more?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.