0

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?

eligolf
  • 1,682
  • 1
  • 6
  • 22
  • Code outside of `` is sent to the browser. Code inside is run *when the document loads*. (Typically this will generate dynamic output to write to the browser). If you want some PHP to run in response to some JavaScript, then put the PHP on another URL and use Ajax. – Quentin Aug 11 '22 at 11:06
  • I understand, did not know that. I am a newbie when it comes to wordpress programming... I really don't get what you mean with another URL and Ajax, if you could please answer this question then I would be very grateful. The link suggested as answer is probably good, I just can't undersstand what and where I need to do things. – eligolf Aug 11 '22 at 12:42

1 Answers1

-2

in

<button type="submit" name="wp-submit" onclick="register_process();" data-ppt-btn class="mt-5">
<?php echo __("Create Account"); ?>
</button>

you only need to remove the "();" in onclick="register_process();"

Mahmoud Hassan
  • 328
  • 1
  • 8
  • If you do that, then when the `onclick` function runs, it won't do anything. You need the `()` in order for `register_process` to be called. Just mentioning a variable, even if it contains a function, does nothing. – Quentin Aug 11 '22 at 11:04
  • I said I tried this and what went wrong with it, sorry this is not helping – eligolf Aug 11 '22 at 12:13