Pass object (inputs values from piotnet form) with AJAX to populate in other page of WordPress

68 Views Asked by At

I have my JS code getting the values from piotnet form and making necessary calculations, after i populate the calculation and values from inputs to my object, then i launch Ajax The piotnet is front end form, which doesn't populate values in mysql.

 $('BUTTON').on("click", function(){
                  /// for test reason i get only 2 values from form
                  var newsRev = {  
                    ndatego: sReservation.dateGo, 
                    ndateback: sReservation.dateBack
                }                      
                        var json= JSON.parse(JSON.stringify(newsRev));                  
                console.log(json);
                        jQuery.ajax({
                              type:'POST',
                              url:'https://mywebsite.com/test/', /// my elementor page with Code snippets shortcode
                              data: {json:json},                           
                              success: function(data) {console.log(data);}, /// getting as result
                              error: function(errorThrown){
                                 console.log(errorThrown);                
                              }
                        })
                }) 

and in my shortcode, in page /test/ i have code

if($_SERVER['REQUEST_METHOD']=='POST'){
    function (){
    if(isset($_POST)){
        $sre = $_POST['json'];
            echo $sre;
            print_r ($_POST); // showing nothing
    }
    die();
}
}

but its not working.. Thank you.....

I am new to php.... The target is to populate the calculations and input values in other wordpress page only for admin and also populate them in phpmyadmin database.

1

There are 1 best solutions below

0
Marishka On

So i have worked it out, maybe will be useful for somebody.

$(document).on('click', '#pafe-form-builder-trigger-success-myFormId', function (ev) {

               ev.preventDefault();
               sReservation = JSON.parse(JSON.stringify(sReservation));
               jQuery.ajax({
                   url: '/wp-admin/admin-ajax.php',
                   cache: false,
                   type: 'POST',
                   data: {
                       'action': 'reservation_system',
                       'reservation_ajax': sReservation
                   }, success: function ($reponse) {
                       console.log("success");
                       doRedirect();
                   },
                   error: function (xhr, status, error) {
                   }
               });
           });

and in my function.php in child theme:

add_action('wp_ajax_nopriv_reservation_system', 'our_reservation', 10, 1);
add_action('wp_ajax_reservation_system', 'our_reservation', 10, 1);
function our_reservation(){
 if(isset($_POST['reservation_ajax'])){

        $reservation = $_POST['reservation_ajax'];
        $newVar = $reservation['someMyItem'];
}
}