How to send JSON to PHP?

2

How do I send this object by ajax , and read in php ?

  

//Aquicriooobjetocomosinputsvarobj=newObject();varobjectsRadio=$('input:checked').map(function(i){obj.id_varejo=id_varejo;obj.id_pesquisa=idPesquisa;obj.id_pergunta=$(this).attr('name');obj.id_resposta=$(this).val();return{id_varejo:obj.id_varejo,id_pesquisa:obj.id_pesquisa,id_pergunta:obj.id_pergunta,id_resposta:obj.id_resposta};}).get();//Aquiseraoenvioparaphpvarsql=$.ajax({type:'get',url:'dadosPS.php?ps=saveResult',dataType:'json',data:{'dados':objectsRadio},//data:jsonString,beforeSend:function(){//console.log("Before");
    },

    success: function (data, textStatus, jqXHR) {
        // console.log("Success = " + data);
    },

    complete: function () {},

    error: function (jqXHR, textStatus, errorThrown) {
        //console.log("Error"); 
    }
 });

Return update, this is the response from php

Array
(
[dados] => Array
    (
        [0] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 1
                [id_resposta] => 3
            )

        [1] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 2
                [id_resposta] => 8
            )

        [2] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 3
                [id_resposta] => 11
            )

        [3] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 4
                [id_resposta] => 12
            )

        [4] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 5
                [id_resposta] => 13
            )

        [5] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 6
                [id_resposta] => 14
            )

        [6] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 7
                [id_resposta] => 16
            )

        [7] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 8
                [id_resposta] => 18
            )

        [8] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 9
                [id_resposta] => 23
            )

        [9] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 10
                [id_resposta] => 26
            )

        [10] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 11
                [id_resposta] => 29
            )

        [11] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 12
                [id_resposta] => 30
            )

        [12] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 13
                [id_resposta] => 31
            )

        [13] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 14
                [id_resposta] => 32
            )

        [14] => Array
            (
                [id_varejo] => 895
                [id_pesquisa] => 1
                [id_pergunta] => 15
                [id_resposta] => 33
            )

    )

)

In the case I need to insert now into the mysql database with this table structure

  • id ( pk, auto increment )
  • search_id
  • question_id
  • id_respostaid_var
  • This would be the end point for me to finish this question.

        
    asked by anonymous 01.08.2017 / 19:55

    1 answer

    2

    A simple example would be like this ...

    var objArr = []
    
    for(let i = 0; i < 20; i++){
      objArr.push(i)
    }
    
    console.log(objArr)
    
    $.ajax({
      type: 'POST',
      url: 'SUA URL',
      data: objArr,
      success: function(data){
        console.log(data)
      }
    })
        
    01.08.2017 / 21:19