Send multiple values ​​dynamically using Ajax and jQuery

  • I have an unknown number of button clicks. (Generated)
  • Each button is connected to several values ​​that must be sent via an Ajax call.

I found this for a start:

$.ajax({
    type: "POST",
    url: "some.php",
    data: parameters,
    success: function(msg){
    alert("nothing");
}
});

datacontains parameters as far as I know. My options vary depending on which button is pressed.

I guess I could use thissomewhere? But what if I need to send 3 values?

<input type="button" id="unique-1"> <!-- With values 'test', 3 and 5 -->
<input type="button" id="unique-2"> <!-- With values 'doh2', 8 and 6 -->

I use PHP if you need this information.

Thank!

+3
source share
2 answers

fiddle. , , . ajax.

+3

.

- . ( ) html. : , , . php $_GET["data[]"]

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js"></script>

<input type="button" class="mybutton" data-mydata="1,2,really" />

<script>
$('.mybutton').click(function() {
  var parameters = $(this).attr('data-mydata').split(',');
  $.ajax({
      type: "POST",
      url: "some.php",
      data: {data:parameters},
      success: function(msg){
        alert("nothing");
      }
  });  
});
</script>

Fiddle at: http://jsfiddle.net/qdH7s/

0

Source: https://habr.com/ru/post/1787542/


All Articles