简体   繁体   English

如何在 Ajax 中将按钮值作为变量传递?

[英]How do I pass Button Value as a variable in Ajax?

I have a problem.我有个问题。 I want to exchange certain data using PHP, MySQL and Ajax.我想使用 PHP、MySQL 和 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.为此,我总是必须将字段的 ID 传递给我的后端,这样我才能继续使用这个 ID。

How do I pass the value from my button to my URL in Ajax?如何将按钮中的值传递给 Ajax 中的 URL? What do I have to consider?我需要考虑什么? row['id'] is my variable (PHP) row['id'] 是我的变量(PHP)

HTML Code: HTML 代码:

<a class='commentSikayet'>
  <button id='commentSikayet' name='commentSikayet' value='{$row['id']}'>
    Şikayet et
  </button>
</a>

Ajax: Ajax:

$(document).ready(function () {
  $("#commentSikayet").click(function () {
    $.ajax({
      url: 'report_comment.php',
      type: 'POST',
      data: {bar: $("#bar").val()},
      success: function (result) {
        alert('Erfolgreich gemeldet.');
      }
    });
  });
});

Since you're listening for the click event on the button, you can access it via this in your handler function.由于您正在侦听按钮上的单击事件,因此您可以在处理程序 function 中通过this访问它。

Add the name / value pair to your AJAX data option将名称/值对添加到您的 AJAX data选项

$("#commentSikayet").on("click", function (e) {
  e.preventDefault();
  $.post("report_comment.php", {
    bar: $("#bar").val(),
    [ this.name ]: this.value // add name / value in here
  }).done(function (result) {
    alert('Erfolgreich gemeldet.');
  });
});

This will include commentSikayet=rowIdValue in the POST request body which you access on the PHP side via...这将包括您在 PHP 端通过...访问的 POST 请求正文中的commentSikayet=rowIdValue ...

$rowId = $_POST["commentSikayet"];

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"] ):每个按钮都有一个标识当前数据集的data-id属性(id 将通过 PHP 脚本作为$row["id"]提供):

 $("body").on("click","button", function(ev){ ev.preventDefault(); // prevent submitting a form... let data={cmt_id: $(this).data("id"), value: $(this).prev().val()} $.post("https://jsonplaceholder.typicode.com/comments",data).done(function (result) { console.log("Erfolgreich gemeldet:",result); }); });
 <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <div><input type="text" value="some text"> <button name="commentSikayet" data-id="123">Şikayet et</button></div> <div><input type="text" value="some other text"> <button name="commentSikayet" data-id="124">Şikayet et</button></div> <div><input type="text" value="more text"> <button name="commentSikayet" data-id="125">Şikayet et</button></div>

In your backend PHP script (taking the place of the above typicode-URL) you can pick up the values from the $_POST superglobal as在您的后端 PHP 脚本(代替上面的 typicode-URL)中,您可以从$_POST超全局中获取值

$_POST["cmt_id"]; // id value
$_POST["value"];. // actual text content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM