javascript - 使用ajax向服务器发送数据的问题

标签 javascript php html ajax

我已经编写了一段代码,用于从 HTML 数据库中创建一个表,并且我希望在每一行上都有删除和确认按钮。我将整行放入 <form method="post">属性。在 JavaScript 文件中,我读取电子邮件的值并使用 ajax 将其发送到服务器,然后服务器执行该操作。 问题是当我想删除一行或使用每行的按钮确认它时,即使我单击另一行的按钮,它也仅适用于第一行。我用过document.getElementById('email')但它总是返回第一行。这是我的 html 代码:

<table id="example1" class="table table-bordered table-striped">
   <thead>
   <tr>
       <th>نام کاربر</th>
       <th>ایمیل</th>
       <th>تایید</th>
   </tr>
   </thead>
   <tbody>
    <?php
    while ($users = mysqli_fetch_assoc($result_user)) {
    ?>
    <tr>
       <form name="ajax-demo" id="ajax-demo" method="get">
           <td><?php echo $users['name']?></td>
           <td><?php echo $users['email']?></td>
                <input id="email" type="hidden" name="email" value="<?php echo $users['email']?>">
            <td>
                <div class="btn-group">
                  <button onclick="confirmUser()" id="btnConfirm" type="submit" class="btn btn-success btn-flat">
                      تایید
                  </button>
                  <button type="button" class="btn btn-success btn-flat dropdown-toggle" data-toggle="dropdown">
                     <span class="caret"></span>
                     <span class="sr-only">Toggle Dropdown</span>
                  </button>
                  <ul class="dropdown-menu" role="menu">
                  <li>
                     <a onclick="deleteUser()" id="btnDelete">حذف</a>
                  </li>
                  </ul>
                </div>
             </td>
        </form>
     </tr>
    <?php }?>
    </tbody>
</table>

和 JavaScript 代码:

<script>
    function confirmUser() {
        var email = document.getElementById("email").value;
        var xhr;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            xhr = new XMLHttpRequest();
        } else if (window.ActiveXObject) { // IE 8 and older
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var data = "email=" + email;
        xhr.open("POST", "confirm_user.php", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(data);
        xhr.onreadystatechange = display_data;
        function display_data() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    //alert(xhr.responseText);
                    document.getElementById("txtHint").innerHTML = xhr.responseText;
                } else {
                    alert('There was a problem with the request.');
                }
            }
        }
    }
    function deleteUser() {
        var email = document.getElementById("email").value;
        var xhr;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            xhr = new XMLHttpRequest();
        } else if (window.ActiveXObject) { // IE 8 and older
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var data = "email=" + email;
        xhr.open("POST", "confirm_user_delete.php", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.send(data);
        xhr.onreadystatechange = display_data;
        function display_data() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    //alert(xhr.responseText);
                    document.getElementById("txtHint").innerHTML = xhr.responseText;
                } else {
                    alert('There was a problem with the request.');
                }
            }
        }
    }
</script>

最后是我的confirm_user.php代码:

<?php
$conn = mysqli_connect('localhost', 'root', 'root', 'my_scheme');
if (!$conn) {
    die('Connection failed ' . mysqli_error($conn));
}
if (isset($_POST['email'])) {
    $email_user = $_POST['email'];
    $sql = "UPDATE my_scheme.tbl_user SET active='true' WHERE email='".$email_user."'";
    if (mysqli_query($conn, $sql)) {
        echo '<h5 class="headline text-green">کاربر با موفقیت تایید شد!</h5>';
    }else {
        echo "Error: ". mysqli_error($conn);
    }
    exit();
}
else {
    echo "failed";
}

confirm_user_delete.php 代码与前面的代码类似。 请帮我解决这个问题。谢谢

最佳答案

如果我理解你的问题的话。您有数据库输入,可以根据数据库的结果创建表行。但即使您单击第二个元素,它始终会从表行中获取第一封电子邮件的 ID。

原因:你可以简单地这样做

    <?php
$email = 'myemail.com';
?>

<button onclick="confirmUser('<?php echo $email; ?>')"class="btn btn-success btn-flat"> Delete Me </button>

<script>
function confirmUser(email)
{
    console.log("The email to delete is: " + email);
}
</script>

关于javascript - 使用ajax向服务器发送数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57376749/

相关文章:

javascript - 为 Firefox 扩展设置弹出窗口标题

Javascript 循环行为

jquery - 如何从子 Jquery 中找到下一个父对象?

javascript - 如何通过 PHP 检查图像是否存在?

javascript - jQuery 将不在我的 html 字符串中的行添加到表中

javascript - 具有可点击区域的网页图像,将数据发送到服务器脚本并保留在同一页面上,无需刷新

javascript - 如何通过动态键读取值,形成json

php - Zoho API V2 更新记录

php - WordPress 插件 + Composer?

javascript - Ext JS 工具栏和面包屑示例在 JSFiddle 中不起作用