Problems passing js data to php page using ajax jquery

90 Views Asked by At

Hi everyone

After one week searching what could be the problem I have not discovered what could fix it.
I already have done some jquery ajax calls before and I am currently trying to realize a Wordpress pluging.

There I am trying to pass an array in Javascript using Ajax Jquery to a Php page using POST request. Ajax request is correctly achieved ( success and done events are triggered ) but when I am accessing the page $_POST is empty. I even tried in the code below to simply put simple string data but I can't access it in php page.

This is my Jquery code :

$('#Enregistrer').click(function(){
    saveArray= new Array();
    var i=0;
    $('.q1').each(function(){
        if ($(this).hasClass('tablesorter-filter-row')==true){
            // doing nothing
        }
        else{
            saveArray[i]=new Array();
            for (var j=0; j<6; j++){
                if ($(this).children().eq(j).hasClass('m1') || $(this).children().eq(j).hasClass('m2')){
                    var value = $(this).children().eq(j).children('select').val();
                }
                
                else{
                var value = $(this).children().eq(j).html();
                }
                saveArray[i][j]= value;
            }
        i++;
        }
    });
    console.log(saveArray);
    var data = 'test';
    $.ajax({
        url:'../api/send_form.php',
        method:'POST',
        data: { 'data': data },
       
        success: function(resp){
            console.log('ok');
            window.location='../api/send_form.php'; 
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }


    });
});

If you need more details don't hesitate, this is my first post on stackoverflow.I try to be as precise as possible.
Thanks

1

There are 1 best solutions below

5
Chris G On

maybe your attempt is missing the type: 'POST', also added a few more elements

$.ajax({
            dataType: 'html', 
            type: 'POST',
            url:'../api/send_form.php',
            method:'POST',
            data: { data: data },
            processData: false,
            contentType: false,
            success: function(resp){
                console.log('ok');
                window.location='../api/send_form.php'; 
            },
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(thrownError);
            }


        });