javascript - Redirect to another website with sweetalert2 and GET information
问题描述
So I want to redirect using sweetalert2 to, say deleting.php with the ?ID= something. How do I put in the something? Here's my code. The first one is in php echo html so that I can put in everything from the sql database.
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>'
And this is the sweetalert2 code
<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>
Let's say the ID is 4, if possible, I'd like it so it could redirect to deleting.php?ID=4. Thank you!
Also is it possible to activate sweetalert2 from GET information and how?
解决方案
Other than in PHP concatenating strings in JavaScript is not done with a dot. You have to use a +
character to concatenate. Therefor your code has to look like the following example;
window.location = "deleting.php?ID=" + id;
Activating SweetAlert as a JavaScript library by a GET parameter is possible. Just use the native URLSearchParams
object for this.
const urlParams = new URLSearchParams(window.location.search);
const myParam = urlParams.get('myParam');
if (myParam) {
// initialize here
}
推荐阅读
- function - 如何将所需的 System.Random.Next 重载传递给我的 F# 函数?
- android - Kotlin - 使用 mockito 测试 lambda
- tableau-api - 基于下拉列表动态呈现多列
- c# - IEnumerable 文件比较
- javascript - 我应该在我的 ajax jquery 请求的 URL 部分放什么?URL不断返回未定义
- sparql - 如何检索任意维基数据项目列表的所有属性
- javascript - 如何从 for 语句中的异步回调中获取结果
- sql - 在 SQL Server 的维护计划中添加 T-SQL
- office365 - 将办公应用程序作为 iframe 集成到我的 Web 应用程序中
- javascript - 我们可以在单个类中使用 this.props.map 和 this.props.navigation 导航到另一个屏幕吗