Empty array errors out form mail

I’ve got a contact form that works great… as long as a checkbox is checked.

If there’s nothing checked, I get an error in my error_log

26-Apr-2023 15:47:20 America/Los_Angeles] PHP Warning:  Undefined array key "howfind" in /home1/bluehqwi/public_html/contact-us/AjaxForm.php on line 126
[26-Apr-2023 15:47:20 America/Los_Angeles] PHP Fatal error:  Uncaught TypeError: implode(): Argument #1 ($pieces) must be of type array, string given in /home1/bluehqwi/public_html/contact-us/AjaxForm.php:126
Stack trace:
#0 /home1/bluehqwi/public_html/contact-us/AjaxForm.php(126): implode(', ', NULL)
#1 /home1/bluehqwi/public_html/contact-us/AjaxForm.php(262): Ajax_Form->__construct()
#2 {main}
  thrown in /home1/bluehqwi/public_html/contact-us/AjaxForm.php on line 126

Line 126 is:

$howfind = implode(', ', $_POST['howfind']);

Can anyone help me figure out what I need to do so if the person doesn’t want to check a box that the script doesn’t error out?


You simply need to make sure the variable is set and isn’t empty →

$howfind = '';

if (isset($_POST['howfind']) && !empty($_POST['howfind'])) {
    $howfind = implode(', ', $_POST['howfind']);

Sponsor our Newsletter | Privacy Policy | Terms of Service