通过 _POST 在引导模式上发送变量

Sending variable through _POST on boostrap modal

我在 php 中制作了一个时间轴应用程序,允许添加、编辑和删除事件。我想使用模式来请求确认并从 boostrap 测试这个功能,但后来我的问题发生了,因为我无法让它通过表单发送变量。

这会调用每个事件的模式:

<button type="button" class="eliminarEventoModal" data-id="<?php echo $evento['id'] ?>" data-bs-toggle="modal" data-bs-target="#eliminarEvento">

这将从之前的 $evento['id'] 获取到 #idEvento,我从其他 post 在此页面上获取与模态相关的信息,我知道它有效

<script> 
$(document).ready(function() {
   $(".eliminarEventoModal").click(function() {
     $("#idEvento").text($(this).attr('data-id'));
     $("#eliminarEvento").modal("show");
  });
});
</script>

这是模态代码。它询问您是否确定要删除。如果没有,模态关闭并且没有任何反应,如果是,则将表单推送到 editEvento.php(我使用模态的不同文件),事件被删除。

<!-- Modal -->
<div class="modal fade" id="eliminarEvento" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="eliminarEventoLabel">Delete event</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
            <p>Confirm delete event.</p>
        <?php $idEvento='<span id="idEvento"/>'; echo "a".$idEvento."a";?> //<--- line to test if getting the right id only
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-bs-dismiss="modal">Cancelar</button>
            <form class="col-md-auto needs-validation" method="POST" action="/editarEvento.php" >
                <input type="hidden" name="borrar" value="borrar">
                <input type="hidden" name="id" value="<?php echo $idEvento;?>"> //<--- line where it dont work
                <button type="submit" class="btn btn-danger">Eliminar</button>
            </form>
        </div>
        </div>
    </div>
    </div>

表格适用于 borrar 变量,但不适用于 id,但为什么呢?我需要 id 来删除事件,所以我不能删除任何。我已经尝试了此页面中的其他一些 post,但我没有开始工作。我没有使用 javascript 的经验,我认为我的问题可能与误解了与之相关的某些部分有关。

--编辑:来自页面源的模态

<div class="modal fade" id="eliminarEvento" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
        <div class="modal-header">
            <h5 class="modal-title" id="eliminarEventoLabel">Eliminar evento</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
            <p>Confirmar eliminar evento.</p>
        a<span id="idEvento"/>a     </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" data-bs-dismiss="modal">Cancelar</button>
            <form class="col-md-auto needs-validation" method="POST" action="/Aeberion/editarEvento.php" >
                <input type="hidden" name="borrar" value="borrar">
                <input type="hidden" name="id" value="<span id="idEvento"/>">
                <button type="submit" class="btn btn-danger">Eliminar</button>
            </form>
        </div>
        </div>
    </div>
    </div>

这是无效的HTML:

<input type="hidden" name="id" value="<span id="idEvento"/>">

value 属性中的 <> 括号破坏了整体解析。如果你真的想让 value 成为一个完整的 HTML 元素,你需要 HTML-encode 输出:

<input type="hidden" name="id" value="<?php echo htmlentities($idEvento);?>">

虽然还不清楚 为什么 你想让 value 成为一个 HTML 元素,因为它包含的信息本质上是无意义的,并且已经知道你。但如果这是您想要的值,则需要对其进行编码,以便 HTML 解析器不会认为它是 <input> 元素的一部分。


虽然它看起来像 this 是你想要的值:

$evento['id']

您在此处分配给 <span>

$("#idEvento").text($(this).attr('data-id'));

但是:

  • <span> 未作为表单的一部分发布到服务器
  • 您需要 ,而不是 <span> 元素的空副本。

您已经知道如何通过 id 查找元素并在其上设置 属性:

$("#idEvento").text($(this).attr('data-id'));

所以给你的目标元素一个 id 这样你就可以设置它的值:

<input type="hidden" name="id" id="eventoModalId">

和:

$("#eventoModalId").val($(this).attr('data-id'));