Create array with AJAX and send to another PHP page

1

Is there any method of creating an array with AJAX to send the value of the selected input's to another page where PHP will receive? there may be several input's depending on the page, but the page that will receive the values is unique for all

<div class="lista">
	<input type="checkbox" value="msc1_URL" checked>Musica 1</br>
	<input type="checkbox" value="msc2_URL">Musica 2</br>
	<input type="checkbox" value="msc3_URL">Musica 3</br>
	<!-- varias musicas aqui -->
</div>
    
asked by anonymous 04.08.2018 / 21:52

2 answers

1

Using the .map () method with .get() you convert a collection of elements jQuery in a simple JavaScript array:

var musicas = $(".lista input:checkbox:checked").map(function(){
   return this.value;
}).get();

console.log(musicas); // imprime ["msc1_URL", "msc3_URL"]
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="lista">
	<input type="checkbox" value="msc1_URL" checked>Musica 1</br>
	<input type="checkbox" value="msc2_URL">Musica 2</br>
	<input type="checkbox" value="msc3_URL" checked>Musica 3</br>
</div>

In this case you can send to PHP via Ajax using the array stored in the variable musicas as given to be sent:

$.post('pagina.php', { musicas: musicas }, function(data){
    // trata o retorno, se quiser
});
    
04.08.2018 / 23:20
1

Jquery

$.post('paginaQueRecebeMusicas.php', $('#meu-form').serialize(), function(data){
    //callback - Executa algo apos finalizar o envio. Ex. limpar o form
)};

HTML

<form id="#meu-form">
     <!-- inputs -->
</form>
    
04.08.2018 / 21:59