Search code examples
phpformscontactsimplode

Warning: implode(): Invalid arguments passed in /var/www/xxxxx.com/contact.php on line 35


I am getting this warning when the form is submitted without entering any of the fields. The error seems to me from line 35 which uses the implode(). Any help will be really appreciated. Thank you so much for looking. Thank you so much for looking. Thank you so much for looking. Thank you so much for looking.

<?php 
$your_email ='[email protected]';// <<=== update to your email address

session_start();
$errors = '';
$name = '';
$lastname = '';
$phone = '';
$address = '';
$zipcode = '';
$city = '';
$state = '';
$interested_in = array();
$interested_in = '';
$visitor_email = '';
$user_message = '';
$ansday = '';
$hidansday='';
 
$qnday=rand(1,7);

$hidansday=$qnday+1;
if($hidansday==8) {$hidansday=1;}
$whatday=jddayofweek ( cal_to_jd(CAL_GREGORIAN, 1,$qnday, 2012) , 1 );

if(isset($_POST['submit']))
{
    $name = $_POST['name'];
    $lastname = $_POST['lastname'];
    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $zipcode = $_POST['zipcode'];
    $city = $_POST['city'];
    $state = $_POST['state'];
    $interested_in = implode(",", $_POST['interested_in']);
    $visitor_email = $_POST['visitor_email'];
    $user_message = $_POST['message'];
    $hiddenansday= $_POST['hidansday'];
    $ansday = $_POST['ansday'];
    
    $dw1=trim(strtolower($ansday));
    $dw2=strtolower(trim(jddayofweek (cal_to_jd(CAL_GREGORIAN, 1,$hiddenansday, 2012) , 1 )));
                                
    
    ///------------Do Validations-------------
        
    $email = htmlspecialchars($_POST['visitor_email']);
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$visitor_email))
    {
        $errors .= "\n E-mail address not valid ";
    }
    
    if ($dw1!==$dw2)
    {
        $errors .= "\n Please enter valid day for the Security Question";
    }
    
    if(empty($name))
    {
        $errors .= "\n First Name is a required field. ";   
    }
    if(empty($address))
    {
        $errors .= "\n Please enter your address. ";    
    }
    if(empty($zipcode))
    {
        $errors .= "\n Please enter your ZIP Code.  ";  
    }
    if(empty($city))
    {
        $errors .= "\n Please enter your City.  ";  
    }
    if(empty($state))
    {
        $errors .= "\n Please enter your State.  "; 
    }
    
    
    if( !preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone) ) {
    $errors .= "\n Please enter a valid phone number "; 
    }
    
    if(empty($errors))
    {
        //send the email
        $to = $your_email;
        $subject="Web Form";
        $from = $visitor_email;
        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
        
        $body = "A user  $name submitted the contact form:\n".
        
        "First Name: $name\n".
        "Last Name: $lastname \n".
        "Phone: $phone \n".
        "Email: $visitor_email \n".
        "Address: $address \n".
        "Zip Code: $zipcode \n".
        "City: $city \n".
        "State: $state \n".
        "Interested In (Check all that apply): $interested_in \n".
        "Message: \n ".
        "$user_message\n".
        "IP: $ip\n";    
        
        $headers = "From: $from \r\n";
        $headers .= "Reply-To: $visitor_email \r\n";
        
        mail($to, $subject, $body,$headers);
        
        header('Location: thankyou.php');
    }
}


?>

This is the HTML part.

<p>
                        Service Interested In <small>(Check all that apply):</small> <br/>
                        <input name="interested_in[]" type="checkbox" id="interested_in1" value="Internet"/> Internet<br>
                        <input name="interested_in[]" type="checkbox" id="interested_in2" value="Telephone" /> Telephone<br>
                        <input name="interested_in[]" type="checkbox" id="interested_in3" value="Key System" /> Key Systems<br>
                    </p>

Solution

  • If you tick at least one of the checkboxes and submit the form , there will be no error/warning because $_POST['interested_in'] will not be empty.

    However, if you do not tick any of the checkboxes, then system will prompt warning because now $_POST['interested_in'] is empty but you try to use implode() on it.

    Hence, you need to check whether $_POST['interested_in'] has value(s) or not first, say by using isset

    So, change the line

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

    to

    if (isset($_POST['interested_in'])){
        $interested_in = implode(",",$_POST['interested_in']);
    }