I have array = ["h1","h2","h3","h4"] in index.html with JavaScript
how can I send it to PHP file with ajax
I have array = ["h1","h2","h3","h4"] in index.html with JavaScript
how can I send it to PHP file with ajax
In my code answers below, replace "yourfilename.php" with the PHP file you are wanting to post to, and replace "yourArrayInJavascript" with your actual JavaScript array variable.
If you are using jQuery:
$.ajax({
type: "POST",
url: "yourfilename.php",
data:JSON.stringify(yourArrayInJavascript),
success: function(html){
console.log(html);
}
});
If you are wanting a vanilla JavaScript answer:
fetch('yourfilename.php', {
method: 'POST',
body: JSON.stringify(yourArrayInJavascript),
headers: {
'Content-type': 'application/json; charset=UTF-8'
}
})
.then(function (response) {
return response.text();
})
.then(function (data) {
console.log(data);
});