Background
I am creating a register page and want to run a function when the user presses "Create Account" button. I have a button like this:
<button type="submit" name="wp-submit" onclick="register_process();" data-ppt-btn class="mt-5">
<?php echo __("Create Account"); ?>
</button>
Then I have the corresponding function in the same PHP file:
function register_process(){
// Check what type of user from checkbox
if(!jQuery("#user_type").is(':checked')){
<?php
// Headers
$headers = "MIME-Version: 1.0" . "\r\n";
$headers = "Content-type:text/html;charset=UTF-8" . "\r\n";
// Get user email
$user_registration_email = "test987"; //$_POST['user_email'];
// Email to, subject and body
$admin_email_address = get_option('admin_email');
$email_subject = "Test title";
$email_body = "Email: {$user_registration_email}";
// Send email
wp_mail( $admin_email_address, $email_subject, $email_body, $headers);
?>
}
}
Problem
There are multiple problems, but the first is that the function "register_process" runs when I load the page and didn't even press the button. I read on some places that I should remove the () from onclick attribute, and this worked with that issue. However, when pressing the button nothing happens.
I also tried to check if the user_email field is empty and only send email if so. However, then I got no emails at all.
The weird thing is that I have other things in my function such as checks that input field are not empty. These errors/warnings only fires when pressing the button as should, they look like this:
if(!jQuery("#privacypolicy").is(':checked')){
jQuery("#ppt-form-error").addClass('text-danger mb-4').html("<?php echo __("You must accept our privacy policy to join our website.") ?>");
CanContinue =0;
return;
}
How do I get the function to only send emails when the button is pressed?