Send Json result to meta box view

Support MB Views Send Json result to meta box view

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #44384
    David JirverusDavid Jirverus
    Participant

    Hi!

    Hope someone can help me out. I have made a meta box view with a static form. When the visitor fills the form I need to grab the values, send it to the bakend, and then back to the meta box view so I can use the values for doing some calculations depending on the values.

    This is my functions.php, but what do I need to add so I can grab the json result on the meta bow view like
    {{boatlength}}

    function calculate_boats() {
        // Get values from AJAX-request
        $boatlength = $_GET['length'];
        $boatwidth = $_GET['width'];
    
        $result = array(
            'boatlength' => $boatlength,
            'boatwidth' => $boatwidth
        );
    
        // Send as Json back to frontend
        wp_send_json($result);
    }
    add_action('wp_ajax_calculate_boats', 'calculate_boats');
    add_action('wp_ajax_nopriv_calculate_boats', 'calculate_boats');
    #44389
    PeterPeter
    Moderator

    Hello David,

    MB Views uses Twig to write the code in the editor, it is the PHP template engine and is rendered when the page load. If you use Ajax to send JSON data to frontend, you have to use JavaScript to handle the data and make changes there.
    You can read more about Ajax in this article https://www.smashingmagazine.com/2011/10/how-to-use-ajax-in-wordpress/

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.