1

I am trying to incorporate Google's ReCaptcha API with PHPMailer, however, I am being faced with 'Undefined index:' error. I am using 'if (isset($_POST['submit']))' so unsure of why this error is being produced.

error:

Undefined index: g-recaptcha-response in C:\xampp\htdocs\contact-form-ajax\contactForm.php on line 15

Please, could someone share some light on this and help with a solution?

I have only pasted in the first part of the code, however, if you require more I can add :)

if(isset($_POST['submit'])) {

  require 'dist/PHPMailer/PHPMailerAutoload.php';
  $mail = new PHPMailer;

  $name = $_POST['name'];
  $email = $_POST['email'];
  $subject = $_POST['subject'];
  $phone = $_POST['phone'];
  $company = $_POST['company'];
  $message = $_POST['message'];
  $secretKey = "--KEY--";
  $responseKey = $_POST['g-recaptcha-response'];
  $userIP = $_SERVER['REMOTE_ADDR'];

  $url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
  $response = file_get_contents($url);
Synchro
  • 35,538
  • 15
  • 81
  • 104
Coder
  • 809
  • 1
  • 10
  • 23
  • 1
    Possdibly the problem is here: `$_POST['REMOTE_ADDR']`. Usually it should look like `$_SERVER['REMOTE_ADDR']`. Probably your post data has no value with index `REMOTE_ADDR` – Eugene Anisiutkin Jan 21 '19 at 07:57
  • Eugene you should answer it. It’s pretty much that issue. – msphn Jan 21 '19 at 08:03
  • amending to $_SERVER['REMOTE_ADDR'] has resolved 'Undefined index: REMOTE_ADDR', however, 'Undefined index: g-recaptcha-response' error is still being produced, any ideas? – Coder Jan 21 '19 at 08:05
  • @Coder, `var_dump($_POST)` will get you everything that your `$_POST` var contains. As of right now it has no index `g-recaptcha-response`. Possible duplicate of [“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Eugene Anisiutkin Jan 21 '19 at 08:22
  • @EugeneAnisiutkin thanks for the reply, apologies for my lack of experience here. So if I am understanding right $responseKey cannot find a value? my contact form is separate from my index.php file - could this be an issue, also I am using Ajax. Sorry, I know its kind of a needle in a haystack question but just looking for some direction :) ? – Coder Jan 21 '19 at 08:43
  • 1
    @Coder, I suck at explaining, but the best guess i can give without seeng contact form and `AJAX`, is that not all desired data is being sent with `POST` . The things you should check first is your contact form ReCaptcha and your AJAX. ReCaptcha response somehow doesn't get sent with all the other POST data. – Eugene Anisiutkin Jan 21 '19 at 08:56

0 Answers0