0

I'm hosting a website from 000webhost.com and when I try to test my contact form, i get this error from the line $errors = [];. I really need your help badly!

session_start();

require_once '../../lib/mailer/PHPMailerAutoload.php';

 $errors = [];

 if(isset($_POST['fname'], $_POST['lname'], $_POST['email'], $_POST['contactno'], $_POST['message'])){
  $fields = [
    'fname'=>$_POST['fname'],
    'lname'=>$_POST['lname'],
    'email'=>$_POST['email'],
    'contactno'=> $_POST['contactno'],
    'message'=> $_POST['message']
    ];

    foreach($fields as $field => $data){
      if (empty($data)){
        $errors[]='The ' . $field . ' field is required.';
      }
    }

    if(empty($errors)){
      $m = new PHPMailer;

      $m->isSMTP();
      $m->SMTPAuth = true;

      $m->Host = 'smtp.gmail.com';
      $m->Username = 'icyraud@gmail.com';
      $m->Password = 'giantboq1';
      $m->SMTPSecure = 'ssl';
      $m->Port = 465;

      $m->isHTML();

      $m->Subject = 'Contact Form Submitted';
      $m->Body = 'From: ' . $fields['fname'] .' '. $fields['lname']. ' (' . $fields['email'] . ')<p>' . $fields['message'] . '</p>';

      $m->FromName = 'Contact';
      $m->AddAddress('icyraud@gmail.com','JDGS Company');

      if($m->send()){
        header('Location: thanks.php');
        die();
      } else{
        $errors[]= 'Sorry, could not send email. Try again later.';
      }
    }

} else{
$errors[] = 'Something went wrong';
}

 $_SESSION['errors'] = $errors;
 $_SESSION['fields'] = $fields;
 header('Location: contact-us.php');
user3744076
  • 127
  • 6
  • 15

2 Answers2

1

You can't define an array in PHP (< 5.4) by simply specifying [], you must use the following code to define your $errors array:

$errors = array();
BenM
  • 52,573
  • 26
  • 113
  • 168
  • On certain versions: _As of PHP 5.4 you can also use the short array syntax, which replaces array() with []._ Taken from [Arrays](http://php.net/manual/en/language.types.array.php) – FirstOne Mar 05 '16 at 16:13
1

[] is only supported in PHP > 5.4 use array() instead.

You will also need to update your $fields to use array() also:

$fields = array(
    'fname'=>$_POST['fname'],
    'lname'=>$_POST['lname'],
    'email'=>$_POST['email'],
    'contactno'=> $_POST['contactno'],
    'message'=> $_POST['message']
    );
digout
  • 4,041
  • 1
  • 31
  • 38