首页 > 解决方案 > 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?

标签: javascriptphphtmljsonsweetalert2

解决方案


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
}

推荐阅读