Send 2 more variable arrays using ajax

0

Good, it happens that I have 2 arrays and 2 variables, which I need to send via POST, so far I have this:

$.ajax({
            type: "POST",
            url: "saveAsistencia.php",
            data: {'array':JSON.stringify(asistencia)},
            dataType: "html",
            error: function(){
            alert("error al hacer consulta");
            },
            success: function(data){ 

            $("#response").empty();
            $("#response").append(data);                                                             
            }
      }); 
   }

that's to send an array I understand, but if I want to send 2 more variable arrays, how should I do it? thanks for the answers:)

EDIT : Excuse me, I missed adding, how do I get it in php? thanks again

    
asked by alexi gallegos 19.12.2017 в 23:43
source

4 answers

0

AJAX

Asynchronous JavaScript and XML ( AJAX ) is not a technology by itself, it is a term that describes a new way of using together several existing technologies. This includes: HTML or XHTML, CSS, JavaScript, DOM, XML, XSLT, and the XMLHttpRequest object. When these technologies are combined in an AJAX model, it is possible to achieve web applications capable of continuously updating without having to reload the entire page. This creates faster applications with better response to user actions.

What is AJAX?

AJAX (Asynchronous JavaScript and XML) is a new term to describe two capabilities of browsers that have been present for years, but that had been ignored by many Web developers, until recently that applications such as Gmail, Google suggest and Google Maps.

The two capacities in question are:

  • The ability to make requests to the server without having to reload the page.
  • The possibility to analyze and work with XML documents.

javascript

 //arrays
 var array1 = ['1','2']
 var array2 = ['3','4']

 //variables
 var variable1 = "hola mundo1"
 var variable2 = "hola mundo2"


/*JSON a Enviar*/
 var datos = {
   'array1':array1,
   'array2':array2,
   'variable1':variable1,
   'variable2':variable2
 }

 $.ajax({
        type: "POST",
        url: "saveAsistencia.php",
        data: datos,
        dataType: "json",
        error: function(){
        alert("error al hacer consulta");
        },
        success: function(data){ 

        $("#response").empty();
        $("#response").append(data);                                                             
        }
   }); 
}

PHP

 <?php
   echo $_GET["array1"]
 ?>

The data of the success should show the same array that you passed as a parameter I hope you help greetings

    
answered by 20.12.2017 / 00:14
source
1

You can do this by adding them to the data property of the ajax.

$.ajax({
            type: "POST",
            url: "saveAsistencia.php",
            data: {'array':JSON.stringify(asistencia), 'array2':JSON.stringify(asistencia2), 'variable':valor1, 'variable2': valor2},
            dataType: "html",
            error: function(){
            alert("error al hacer consulta");
            },
            success: function(data){ 

            $("#response").empty();
            $("#response").append(data);                                                             
            }
      }); 
   }
    
answered by 19.12.2017 в 23:46
0

To send multiple data by AJAX just complete the JSON structure that you already have, like this:

$.ajax({
        type: "POST",
        url: "saveAsistencia.php",
        data: {
         'array1' : JSON.stringify(asistencia),
         'array2' : JSON.stringify(asistencia),
         'variable1' : variable1,
         'variable2' : variable2
        },
        dataType: "html", 
        error: function(){
           alert("error al hacer consulta");
        },
        success: function(data){ 
           $("#response").empty();
           $("#response").append(data);                                                             
        }
  }); 

}

Remember to define the parameter async at the time of making the request to define whether to make it asincrona or sincrona .

    
answered by 19.12.2017 в 23:48
0

It would be as follows:

$.ajax({
            type: "POST",
            url: "saveAsistencia.php",
            data: {
                   array1:JSON.stringify(arreglo1),
                   array2:JSON.stringify(arreglo2),
                   variable1: "valor1",
                   variable2: "valor2"
            },
            dataType: "html",
            error: function(){
            alert("error al hacer consulta");
            },
            success: function(data){ 

            $("#response").empty();
            $("#response").append(data);                                                             
            }
      }); 
   }
    
answered by 19.12.2017 в 23:52