To send a form in get and post I use this form:

<form name="send" action="/example.php?id=1" method="post" accept-charset="utf-8"> <p> <b>Удалить куки: </b><input type="checkbox" name="delete_cookie" value="1"> </p> <input type="text" name="id" value="3"> <input type="submit" value="Отправить"> </form> 

I need to use jQuery for some input for example with class="get-key" , to send with get . That is what I do, I create input with a class. Then all the values ​​using .each I'm going into an array, let's say id and value. The only problem is that how do i use jQuery to put all id and value that I collected from input 's into action="example.php?id=val....&id[n]=val[n] .

    2 answers 2

    To send a get request to jQuery, there is a method of the same name . For example:

     $.get(adress); 

    However, in the case of jQuery, it is best not to insert variables directly into the address bar, but to generate data in JSON or XML format

      As you already wrote: I create input with the class class="get-key" . Further, with the help of .each I collect all the values ​​into an array, let's say id and value . After that, we convert the prepared data as an array into JavaScript using JSON.stringify into a JSON string:

       data = JSON.stringify(dataArray); // dataArray - подготовленные данные 

      After the data is prepared, you can send via jQuery.ajax() :

       $.ajax( { // ajax запрос type: "GET", // с помощью get url: 'example.php', // по адресу data: { array: data }, // отправляемые данные success: function(response) { // в случае успеха console.log(response); }, error: function(response) { // в случае провала console.log(response); }, }); 

      jQuery.ajax() - performs an asynchronous HTTP(Ajax) request. You can also use jQuery.get() to load data from the server using an HTTP-GET request.

      • Actually, where is it written in the links listed by you that $ .get is used only to load data from the server? For me, all 3 functions for working with Ajax are just different wrappers for XMLHttpRequest and, in fact, it’s not so important $ .get $ .post unless of course delving into the difference between the methods - lazyproger
      • @Dmitry Prikhodchenko, the author of the question indicated: send using get . Regarding your question, I did not write only . Open the link: jQuery.get () and find the text there: Download data from the server using an HTTP GET request . Open the link: jQuery.ajax () and find the text there: Performs an asynchronous HTTP (Ajax) request . There it is all there. I brought the links for a reason and for studying and confirming what I wrote. - Denis Bubnov