N
N
Ninja Mate2016-01-28 16:59:54
JavaScript
Ninja Mate, 2016-01-28 16:59:54

How to send AJAX request from ReactJS form if Wordpress back-end?

I wrote a small pop-up form in reactjs (attached to buttons by class) and compiled js from it for use in a wp template. The question arose how to send a request to the WP engine.
I used wp_mail in a regular template, but then I ran into the need for an AJAX request (I didn’t use it before). Help me to understand.
The following method is attached to the form (instead of the standard submit, onClick is made)

getFormData() {
        var data = {
            name: this.refs.name.getDOMNode().value,
            mail: this.refs.mail.getDOMNode().value,
            text: this.refs.text.getDOMNode().value
        };

        $.ajax({
            action: 'send_message',
            url: "<?php echo admin_url('admin-ajax.php'); ?>",
            name: data.name,
            email: data.mail,
            message: data.text
        })

    },


What exactly should getFormData() return somewhere or something (how to make ajax work) do?
It is clear that you need a method to make sendFormData (), but now the question is somewhat different ....
Tell me who invented this already)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Vorotnev, 2016-01-28
@victorzadorozhnyy

You don't have to invent anything.
1. Send data
2. On the PHP side, intercept the sending using the specified action
3. Do what you need and return a json response
4. Process the results on the success of the Ajax request
More details: https://premium.wpmudev.org/blog/using -ajax-with-w...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question