Data being saved to the database before save to table call made

Thanks Zuluru, I changed it. I have
<?php echo $this->Form->hidden('g-recaptcha-response', ['id'=>'g-recaptcha-response', 'name'=>'response', 'class'=>'g-recaptcha-response']);?> in the HTML and I don’t know if the name being set to “response” matters. It’s the same as var response = grecaptcha.getResponse(); in the callback and maybe that will lead to conflicts? I tried changing the name to ‘g-recaptcha-response’ but that just didn’t work.

The name of the input field and the name of the variable have nothing to do with each other. Variables are variables, you can name them whatever you want, there’s no black magic that will change the value of some HTML just because you happened to name a JavaScript variable the same.

If you have that hidden input being created, and the value of it is not being updated when your enableBtn function is called (or maybe you’ve renamed that function now to LoginButton for some reason?), then the problem lies in that function. Show us exactly what HTML is currently being generated for that input, and the latest version of the captcha initialization code and the callback you’re registering (you change so many things from one post to the next that we can’t trust that code you shared yesterday is still remotely the same), and what outputs you see in either alert boxes or your browser’s JS console.

<?php echo "<div class='g-recaptcha' data-sitekey='" . $recaptcha_user . "' data-callback='enableBtn'></div>"; ?>
<?php echo $this->Form->hidden('g-recaptcha-response', ['id'=>'g-recaptcha-response', 'name'=>'response', 'class'=>'g-recaptcha-response']);?>
<?php echo $this->Form->submit(__('Register'),['class'=>'input_field button submit', 'value' => 'submit', 'disabled'=> 'disabled', 'id'=>'button1']);  ?>
function enableBtn() {
  var response = grecaptcha.getResponse();
  var $result = $('#myParagraph');
  if(response.length === 0) { 
    $result.text("reCaptcha not verified"); 
  }
  else { 
    $result.text("reCaptcha Verified");
    document.getElementById("g-recaptcha-response").setAttribute('value', response);
    document.getElementById("button1").disabled = false;
  }
}

in the head:


<script src="https://www.google.com/recaptcha/api.js" async defer></script>

In my controller:


$recaptcha_user = Configure::consume('recaptcha_user');
$this->set(compact('recaptcha_user'));
	
if($this->request->is('post')){
$recaptchaSecret = Configure::consume('recaptcha_secret');
	
$recaptchaResponse = $this->request->getData('g-recaptcha-response');
	
$google_url = "https://www.google.com/recaptcha/api/siteverify";
       
$response = file_get_contents($google_url . '?secret=' . $recaptchaSecret . '&response=' . $recaptchaResponse);
//DEBUG HERE
//debug($this->request);
//die();
 		 
$responseData = json_decode($response, true);	 

$usersTable = $this->fetchTable('Users');
$user = $usersTable->newEmptyEntity();

if( $responseData["success"] === true  && $usersTable->save($user) ) {
		//Send an email to verify the new account

And with this current code, it’s saying “reCaptcha Verified” or “reCaptcha not verified”, or does that not update at all? Does the g-recaptcha-response hidden input get filled in if you inspect it with browser tools? (Might be easier to see whether it’s being filled in if you temporarily change it from a hidden input to an actual control?) Does the “Register” button get disabled? Do you get any error messages in the JS console in your browser?

When I click on Register without clicking the recaptcha, it doesn’t show the message “reCaptcha not verified” but when I click on recaptcha it does show “reCaptcha Verified.” I made the g-recaptcha-response hidden input into a textarea at first to see that it’s being filled in and then changed it back to a hidden input. I also saw it was getting filled in when I used debug($this->request); in my controller. None of my error messages for the form inputs show up until after I click the recaptcha button. For example, passwords don’t match and name is required don’t show up as error messages until reCaptcha is submitted. Im my JS console, I got the message: Found 2 elements with non-unique id #g-recaptcha-response textarea and hidden input. When I looked to see if it was an error or a warning, I couldn’t find the message any more. The “Register” button does get disabled.

I went back to an older post to add <p id="myParagraph"></p> to the HTML but I wasn’t allowed to edit the post. For some reason, one can only edit one’s post for a certain amount of time after posting. I would be interested to know the reasoning behind this.

So, the data is now being filled in and getting to the controller in debug($this->request);?

Yes, Thanks for the help Zuluru, Kevin Pfeifer, and jarekgol.