【问题标题】:Redirect to another website with sweetalert2 and GET information使用 sweetalert2 和 GET 信息重定向到另一个网站
【发布时间】:2020-07-09 12:16:19
【问题描述】:

所以我想使用 sweetalert2 重定向到,比如删除 .php 和 ?ID= 东西。我怎么把东西放进去?这是我的代码。第一个是在 php echo html 中,这样我就可以从 sql 数据库中放入所有内容。

echo'
<td style="text-align: center;"> <div class="btn-group dropup">
<button class="btn btn-info dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Action
</button>
   <div class="dropdown-menu dropdown-menu-right" aria-labelledby="dropdownMenuButton">
      <a class="dropdown-item" href="edit_form.php?ID='.$operatives['ID'].'">Edit</a>
      <a class="dropdown-item red" onclick="oof("'.$operatives['ID'].'")" href="#">Purge</a>
   </div>
</div> </td>'

这是 sweetalert2 代码

<script>
function oof(id){
    var MyId = id;
    Swal.fire({
  title: 'Are you sure?',
  text: "You will be purging this person's account.",
  icon: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#d33',
  cancelButtonColor: '#3085d6',
  confirmButtonText: 'Yes, purge account'
}).then((result) => {
  if (result.value) {
    window.location = "deleting.php?ID="+MyId;
  }
})
}
</script>

假设 ID 为 4,如果可能,我希望它可以重定向到 delete.php?ID=4。谢谢!

也可以从 GET 信息中激活 sweetalert2 吗?如何激活?

【问题讨论】:

  • 你的问题不清楚,你到底想要什么?
  • 我想将信息从 php 传递给 sweetalert 的函数,以便它可以使用该函数将我重定向到具有 ?id=something 信息的另一个站点
  • 在 JavaScript 中连接字符串是通过 + 而不是 . 完成的。所以你的重定向必须看起来像window.location = "deleting.php?ID=" + id;。就是这样。

标签: javascript php html json sweetalert2


【解决方案1】:

除了 PHP 之外,JavaScript 中的字符串连接不是用点来完成的。您必须使用 + 字符进行连接。因此,您的代码必须类似于以下示例;

window.location = "deleting.php?ID=" + id;

可以通过 GET 参数将 SweetAlert 激活为 JavaScript 库。只需为此使用本机 URLSearchParams 对象。

const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');

if (myParam) {
    // initialize here
}

【讨论】:

  • 谢谢! + 起作用了!如果你不介意我问,当我现在单击它时,当我向函数添加参数时,即使我添加了 var MyId,sweetalert2 似乎也没有运行。有什么想法吗?提前谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 2021-11-19
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
相关资源
最近更新 更多