Jump to content

Undefined array key error


Go to solution Solved by maxxd,

Recommended Posts

Hello there, 

I use below code snippet in woocommerce to confirm emails. It creates a php error as below. How I can solve it?

"[09-Nov-2022 07:55:09 UTC] PHP Warning: Undefined array key "confirm_email" in /srv/htdocs/wp-content/themes/generatepress_child/functions.php on line 846"

 

 

/**
* Snippet Name:		WooCommerce Confirm Email On Registration Form
* Snippet Author:	ecommercehints.com
*/
// Create the field and display it on the registraiton form
add_action('woocommerce_register_form', 'ecommercehints_registraion_form_confirm_email_field');
function ecommercehints_registraion_form_confirm_email_field() {
   woocommerce_form_field(
      'confirm_email',
      array(
         'type'        => 'text',
         'required'    => true,
         'label'       => 'E-posta adresi tekrar',
     ),
      (isset($_POST['confirm_email']) ? $_POST['confirm_email'] : '')
  );
}
// Show an error message if the confirm email field is empty or doesn't match the billing email field
add_action('woocommerce_register_post', 'ecommercehints_confirm_email_field_validation', 10, 3);
function ecommercehints_confirm_email_field_validation($username, $email, $errors) {
   if (empty($_POST['confirm_email'])) {
      $errors->add('confirm_email_error', 'Lütfen e-posta adresinizi iki alana da girin!');
   }
   if ( $email !== $_POST['confirm_email'] ) {
   $errors->add('confirm_email_error', 'Girdiğiniz e-posta adresleri birbiri ile aynı değil!');
   }
}

 

Link to comment
https://forums.phpfreaks.com/topic/315524-undefined-array-key-error/
Share on other sites

  • Solution

In your function ecommercehints_confirm_email_field_validation() (yeesh, wordpress really promotes bad ides...), your second if() statement needs to be an elseif() branch. The reason is that in the first if() you're checking to make sure $_POST['confirm_email'] is set, which is correct. In the second, however, you're assuming it is set by trying to compare it to another value. So if you make it an elseif branch it'll only run when the variable is set, which is the behavior you want.

2 hours ago, maxxd said:

In your function ecommercehints_confirm_email_field_validation() (yeesh, wordpress really promotes bad ides...), your second if() statement needs to be an elseif() branch. The reason is that in the first if() you're checking to make sure $_POST['confirm_email'] is set, which is correct. In the second, however, you're assuming it is set by trying to compare it to another value. So if you make it an elseif branch it'll only run when the variable is set, which is the behavior you want.

Thanks for reply. So you suggest to change this code

if ( $email !== $_POST['confirm_email'] ) {

to this one, right?

elseif ( $email !== $_POST['confirm_email'] ) {

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.