Modifying my query
I have a form with submit functionality without refreshing the page, however this form is embedded within a foreach loop
Where should I place the JavaScript code? Inside the foreach loop or outside?
If it's placed outside, how can I assign a unique identifier to it?
<script>
const myForm = document.getElementById('follow<?php echo $card['user_id']; ?>');
myForm.addEventListener('submit', function(e){
e.preventDefault();
const formData = new FormData(this);
fetch('../config/follow.php', {
method: 'post',
body: formData
}).then(function(response){
return response.text();
}).then(function(text){
//alert(text);
$("#update").load(window.location.href + " #update" );
}).catch(function(error){
//alert(error);
alert('The operation could not be completed');
});
});
</script>
<form method="POST" id="follow<?php echo $card['user_id']; ?>">
<input type="hidden" name="target" value="php $card['user_id'] ">
<div id="update">
<!-- <button change after submit (0 or 1)-->
</div>
</form>