WordPress - Execute onclick function on button when page loads
P粉575055974
P粉575055974 2023-08-30 00:33:02
0
1
649
<p><strong>Background</strong></p> <p>I'm creating a registration page and want to run a function when the user presses the "Create Account" button. I have a button like this: </p> <pre class="brush:php;toolbar:false;"><button type="submit" name="wp-submit" onclick="register_process();" data-ppt-btn class ="mt-5"> <?php echo __("Create account"); ?> </button></pre> <p>Then I have the corresponding function in the same PHP file: </p> <pre class="brush:php;toolbar:false;">function register_process(){ // Check the user type in the checkbox if(!jQuery("#user_type").is(':checked')){ <?php // Header $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']; // Recipients, subject and body of the email $admin_email_address = get_option('admin_email'); $email_subject = "Test subject"; $email_body = "Email: {$user_registration_email}"; // send email wp_mail( $admin_email_address, $email_subject, $email_body, $headers); ?> } }</pre> <p><strong>Question</strong></p> <p>There are multiple issues, but the first is that the function "register_process" is running when I load the page, without even pressing the button. I read somewhere that the () should be removed from the onclick attribute and this solved the problem. However, nothing happens when the button is pressed. </p> <p>I also tried checking if the user email field is empty and only sending the email if it is empty. However, this way I don't receive any emails at all. </p> <p>Oddly enough, I have a few other things in my function, like checking if the input field is empty. These errors/warnings only fire when the button is pressed, and they look like this: </p> <pre class="brush:php;toolbar:false;">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 site .") ?>"); CanContinue =0; return; }</pre> <p>How can I make the function only send the email when the button is pressed? </p>
P粉575055974
P粉575055974

reply all(1)
P粉212114661

Onclick="register_process();" only needs to remove "();"

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template