I have a list of extracted data, and for each of them there is a form below, like a comment box form, where other users can leave a message for specific data.
What I have tried so far is to put the submit button in an array to distinguish it from one to the other (I donβt know if I am doing this correctly, as this is one of my first attempts to use the Javascript / jQuery library and AJAX).
I can send the data and paste it into the SQL database, simply using PHP / MySQL, but I wanted to achieve at least a comment-like system of this community, Stackoverflow, in which, after the comment was posted, it appeared immediately after the button was clicked (without restarting the whole page to send data to the database).
This is dynamically sent data:
<?php while($loopquery){ ?> <div> <?php echo $row['data']; ?> <div id="flash[]"></div> <form action="#" method="POST"> <input type="text" name="comment[]" id="comment[]"> <input type="submit" id="submit[]"> </form> </div> <?php } ?>
And as soon as the submit button is pressed:
$(function () { var submit = document.getElementById('submit'); for (var a = 0; a < submit.length; a++) { submit[a].click(function () { var comment = document.getElementById('comment'); var hiddentaskid = document.getElementById('hiddentaskid'); var dataString = '&comment='+comment[a]+'&hiddentaskid='+hiddentaskid[a]; if (comment[a] == '' || hiddentaskid[a] == '') { alert('Please Give Valid Details'); } else { var flash = document.getElementById('flash'); flash[a].show(); flash[a].fadeIn(400).html('Loading message'); $.ajax({ type: "POST", url: "commentajax.php", data: dataString, cache: false, success: function (html) { $("ol#update").append(html); $("ol#update li:last").fadeIn("slow"); flash[a].hide(); } }); } return false; }); } });
I tried using an array to distinguish data from one to another, but it does not work when I try to configure a script. A new posted post / comment should appear in the <div ="flash"></div> (using AJAX ). Can someone help me how can I achieve the desired result?
source share