最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Ajax Form Issues using Lightsail (AWS)

programmeradmin1浏览0评论

I am having issues with a contact form using Ajax and jQuery.

I followed this guide: , and it works on my local server (Laragon), by that laragon catches the email being sent to me.

But using the contact form in my live server doesn't seem to send the form to my admin_email. I already made sure the admin email of my wordpress site is my own email address.

Here is the code for the form (jQuery and Ajax only, no HTML):

<script>
    (function($) {
        $('#FormControlSelectService').change(function() {
            // Shows the Online Program Options if Online Training is chosen
            ($(this).val() == 'Online' ? $('#OnlineSelectProgram').show() : $('#OnlineSelectProgram').hide());
        });

         

        $('#enquiry').submit( function(event) {
            event.preventDefault(); // Allows the event to be cancelled if it is cancelable

            var endpoint = '<?php echo admin_url('admin-ajax.php'); ?>';
            var form = $('#enquiry').serialize(); // Serializes all the data entered
            var formdata = new FormData;

            formdata.append('action', 'enquiry');
            formdata.append('nonce', '<?php echo wp_create_nonce('ajax-nonce');?>');
            formdata.append('enquiry', form);

            $.ajax(endpoint, {
                type: 'POST',
                data: formdata,
                processData: false,
                contentType: false,

                success: function(res) {
                    $('#enquiry').fadeOut(200);
                    $('#success_message').text('Thank You for Your Enquiry').show();
                    $('#enquiry').trigger('reset');
                    $('#enquiry').fadeIn(500);
                },

                error: function(err) {
                    alert(err.responseJSON.data);
                }
            })
        });

    })(jQuery) </script>

And the code for it in my functions.php:

add_action('wp_ajax_enquiry', 'enquiry_form');
    add_action('wp_ajax_nopriv_enquiry', 'enquiry_form');
    /**
     * 
     * Ajax Form
     */
    function enquiry_form() 
    {
        if (!wp_verify_nonce($_POST['nonce'], 'ajax-nonce')) {
            wp_send_json_error('Nonce is incorrect', 401);
            die();
        }

        $formdata = [];
        wp_parse_str($_POST['enquiry'], $formdata);

        // Sending the Email
        $admin_email = get_option('admin_email');

        // Email Headers
        $headers[] = 'Content-Type: text/html; charset=UTF-8';
        $headers[] = 'From: My Website <' . $admin_email . '>';
        $headers[] = 'Reply-to: ' . $formdata['email'];

        // Sending Email To
        $send_to = $admin_email;

        // Subject
        $subject = "Enquiry from " . $formdata['fname'] . ' ' . $formdata['lname'];

        // Message
        $message = '';

        foreach($formdata as $index => $field)
        {
            $message .= '<strong>' . $index . '</strong>: ' . $field . '<br/>';
        }

        try {
            if (wp_mail($send_to, $subject, $message, $headers)) {
                wp_send_json_success('Email Sent!');
            } else {
                wp_send_json_error('Email Error');
            }
        } catch (Exception $e) {
            wp_send_json_error($e->getMessage());
        }
    }
发布评论

评论列表(0)

  1. 暂无评论