If I have this HTML:
<form>
<a href="#" data-name="color" data-value="yellow" class="active">
<a href="#" data-name="color" data-value="red">
<a href="#" data-name="color" data-value="orange">
<a href="#" data-name="fruit" data-value="banana" class="active">
<a href="#" data-name="fruit" data-value="apple">
<buton id="send">Send</button>
</form>
How can I create an array with all the <a class="active">
through Ajax to play with them in PHP after ?
Thanks.
2
Answers
You can create normal array and then using each loop add value to that array or you can create JSON Array and push value like key-value pair using same each loop.
Demo Code :
You need to use formData to send the
value
andname
data attribute of element havingactive
class to yourPHP
back end file.There is
NO
point creating anarray
and looping through thatarray
again to send / createformData
for ajax. That is totally unnecessary.Please note: You can NOT send an
array
in anajax
request. So your probably need this solution as per your question.Initialize
formData
and append yourname
andvalue
attribute to it in a$.each
and then send thatformData
usingajax
Working Demo: