将输入元素值(php)传递给Sweetalert javascript

时间:2018-06-15 21:45:04

标签: javascript php jquery html

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

背景 我使用PHP来提取对象ID,我基本上都有输入类型按钮,一旦点击将发出警报以确认是否要继续,一旦确认将更新数据库(例如重新认证)。

实现了"它还提供基本警报。现在我想用SweetAlert替换这个基本警报,所以我不得不更换" a"元素"输入"。

以下是HTML代码



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

以下是JavaScript

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

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

警报没有问题,完全正常。问题是只有表格行中的第一个$ id才会在window.location中传递。

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

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你有几行按钮。您需要传递相应点击按钮的行&#39; recertid / id&#39;通过window.location到你的PHP脚本。

让我们通过&#39; id&#39;功能的价值&#34; myFunction(&#39; $ id&#39;)&#34;

&#13;
&#13;
<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>
&#13;
&#13;
&#13;