I have a problem. I want to exchange certain data using PHP, MySQL and Ajax.
To do this I always have to pass the ID of a field to my backend, so I can continue working with this ID.
How do I pass the value from my button to my URL in Ajax?
What do I have to consider?
row[‘id’] is my variable (PHP)
HTML Code:
<a class='commentSikayet'>
<button id='commentSikayet' name='commentSikayet' value='{$row['id']}'>
Şikayet et
</button>
</a>
Ajax:
$(document).ready(function () {
$("#commentSikayet").click(function () {
$.ajax({
url: 'report_comment.php',
type: 'POST',
data: {bar: $("#bar").val()},
success: function (result) {
alert('Erfolgreich gemeldet.');
}
});
});
});
2
Answers
Since you’re listening for the click event on the button, you can access it via
this
in your handler function.Add the name / value pair to your AJAX
data
optionThis will include
commentSikayet=rowIdValue
in the POST request body which you access on the PHP side via…Assuming there might be more than one data sets in your page I modified your example to the following snippet. Each buttons has a
data-id
attribute that identifies the current dataset (the id would be supplied through your PHP script as$row["id"]
):In your backend PHP script (taking the place of the above typicode-URL) you can pick up the values from the
$_POST
superglobal as