首页 > 解决方案 > 将输入元素值 (php) 传递给 Sweetalert javascript

问题描述

我对编程很陌生,所以希望在这里得到一些答案

背景: 我正在使用提取对象 id 的 PHP,并且我基本上有输入类型按钮,一旦单击该按钮将发出警报以确认您是否要继续,一旦确认将更新数据库(例如重新认证)。

实现了它具有 " 并且它还提供了基本警报。现在我想用 SweetAlert 替换这个基本警报,所以我不得不用 "input" 替换 "a" 元素。

下面是HTML代码

<input type="button" onclick="myFunction();" class="btn btn-info" value="Recertify" />
<input type="hidden" id="recertid" value="<?php echo $id; ?>"/>

下面是JavaScript

<script>
function myFunction(){
  swal({   
    title: "",   
    text: "Are you sure you want to recertify?",   
    type: "warning",   
    showCancelButton: true,
    showConfirmButton: true,   
    confirmButtonColor: "#DD6B55",   
    confirmButtonText: "Recertify!",   
    cancelButtonText: "I am not sure!", 
    closeOnConfirm: false,   
    closeOnCancel: false,
  }, 
    function(isConfirm){   
        if (isConfirm) 
    {   
    	var sid = document.getElementById("recertid").value;
        swal({
          title: "", 
          text: "Schedule recertified!",
          type: "success",
          timer: 1000
        }, function(){
          window.location = "recertify.php?id=" + sid;
        }

        );   
        } else {
            //return false;     
            swal({
              title: "",
              text: "No action taken!",
              type: "error",
              timer: 1000
              });   
            } 
        });
}
</script>

如您所见,使用 window.location 所需的 id 被传递给另一个 php

警报没有问题,工作得很好。问题是只有表格行中的第一个 $id 会在 window.location 中传递。

我们如何根据行中单击的按钮传递值?试图在网上寻找答案,但找不到正确的方法

任何帮助表示赞赏

标签: javascriptphpjqueryhtml

解决方案


如果我理解正确,您有几行带有按钮。您需要通过 window.location 将相应单击按钮的行“recertid / id”传递给您的 PHP 脚本。

让我们将 'id' 值传递给函数“myFunction('$id')”

<input type="button" onclick="myFunction('<?php echo $id; ?>');" class="btn btn-info" value="Recertify" />
<input type="hidden" id="recertid" value="<?php echo $id; ?>"/>

<script>
function myFunction(recertid){
  swal({   
    title: "",   
    text: "Are you sure you want to recertify?",   
    type: "warning",   
    showCancelButton: true,
    showConfirmButton: true,   
    confirmButtonColor: "#DD6B55",   
    confirmButtonText: "Recertify!",   
    cancelButtonText: "I am not sure!", 
    closeOnConfirm: false,   
    closeOnCancel: false,
  }, 
    function(isConfirm){   
        if (isConfirm) 
    {   
        swal({
          title: "", 
          text: "Schedule recertified!",
          type: "success",
          timer: 1000
        }, function(){
          window.location = "recertify.php?id=" + recertid;
        }
        );   
        } else {
            //return false;     
            swal({
              title: "",
              text: "No action taken!",
              type: "error",
              timer: 1000
              });   
            } 
        });
}
</script>


推荐阅读