0

ETA: Someone marked this question as duplicate, but I have combed all of the similar threads and nothing suggested has worked. Adding brackets (as in need[]) did not work - it returns the exact same result as discussed below. If I could have found the answer in another thread, I would not have made a new one. It required me to register for an account and spend time formatting a post, which is time I would have spent working on this website if the answer was already posted here.

If you have an example of a thread with a solution that might work, feel free to link to it, but I have looked at every thread I could find on this website that even remotely seemed related. Thanks.


I've seen this answered several times in several ways, but none of the solutions have worked for my situation, so I'm posting here in hopes that someone can help.

The website is a landing page where prospective clients will fill out a form, which will then be sent to a specified email address. The form contains ten checkboxes of services they, all formatted thus:

<input type="checkbox" name="need" value="svc3">Description of Service 1<br/>
<input type="checkbox" name="need" value="svc2">Description of Service 2<br/>
<input type="checkbox" name="need" value="svc3">Description of Service 3<br/>

And so on and so forth, for 10 options. Any/all options should be able to be selected. In the email handler PHP file, I have this code:

$name = $_POST['name']; 
$email_address = $_POST['email'];
$location = $_POST['loc']; 
$age = $_POST['age'];
$gender = $_POST['gender'];
$need = implode(", ", $_POST['need']);

(I originally used just $need = $_POST['need']; but that only provided the last selected variable, not all selected variables.)

$to = $myemail; 
$email_subject = "New Consultation Request: $name";
$email_body = "You have received a new request for a FREE consultation. \n".
"Name: $name \nEmail: $email_address \nLocation: $location \nAge: $age. \nGender: $gender \n".
"\nI would like to work on: $need \n"
"\nMessage: \n$message "; 

$headers = "From: $myemail\n"; 
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);

Everything else sends perfectly. Currently, the email sends with no data in the $need field, as such:

You have received a new request for a FREE consultation.
Name: Mr. Robot
Email: fake@mail.com
Location: Mars
Age: 100
Gender: Robot
I would like to work on:
Message:
Hello, I am a test. Robot voice.

What am I doing wrong?

Roxanne
  • 1
  • 1

1 Answers1

1

You should use [] in the name of the checkboxes to send an array. In your actual code, $_POST['need'] is not an array, so, using implode() returns NULL.

<input type="checkbox" name="need[]" value="svc1">Description of Service 1<br/>
<input type="checkbox" name="need[]" value="svc2">Description of Service 2<br/>
<input type="checkbox" name="need[]" value="svc3">Description of Service 3<br/>

Note that the first element should have svc1 value (instead of svc3).

Syscall
  • 19,327
  • 10
  • 37
  • 52
  • That did not work. It still returns nothing. – Roxanne Apr 15 '18 at 16:23
  • 1
    @Roxanne, appending `[]` after the field name should definitely solve the issue. If it doesn't, there must be something else happening that's not included in the code you've posted. – fubar Apr 15 '18 at 22:01
  • I did eventually find what was wrong with it, and you are correct. The server apparently was not refreshing, so it was still running the code as just `name="need"` rather than `name="need[]"` and thus still returning the same result. I'm not sure how I actually got it to refresh the cache, but I wish I could, because I'm having the same issue with it using a cached stylesheet and not allowing me to make changes. – Roxanne Apr 22 '18 at 15:26