简体   繁体   中英

Function to return true in OnClick with SweetAlert

Hi I'm doing a function in PHP, in which I ask for a confirmation to continue using SweetAlert, the idea is to use the onclick event, but the problem is I have no control, the message is displayed but does not expect confirmation in any of both cases, which are a form and a link.

Code:

<meta http-equiv="content-type" content="text/html; UTF-8" />

<script src="Scripts/jquery-1.4.4.min.js"></script>
<script src="sweetalert/dist/sweetalert-dev.js"></script>
<link rel="stylesheet" href="sweetalert/dist/sweetalert.css">

<body>

<?php

function ShowMessage($titulo,$contenido,$tipo) {
    if($tipo=="ok") {
        //
    }
    else if($tipo=="error") {
        //
    } else if($tipo=="ask") {
echo "
swal({
  title: '$titulo',
  text: '$contenido',
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#DD6B55',
  confirmButtonText: 'Yes, delete it!',
  cancelButtonText: 'No, cancel plx!',
  closeOnConfirm: false,
  closeOnCancel: false
},
function(isConfirm){
  if (isConfirm) {
    return true;
  } else {
    return false;
  }
});
";
    } else {

    }
}

?>

<a href='http://www.google.com' onClick="<?php ShowMessage("test","test","ask"); ?>">Go</a>

<form action="http://www.google.com">
<input type="submit" name="none" text="test" onClick="<?php ShowMessage("test","test","ask"); ?>">
</form>

</body>

Someone could help me?

<?php ShowMessage("test","test","ask"); ?>

Is being called on page load.

You cannot call PHP functions based on browser events without AJAX .

Showing a confirm message would be better done through use of JavaScript.

Try something like

<input type="submit" name="none" text="test" onClick="checkConfirm('test')">
<script>
checkConfirm(message) {
    var confirm = confirm(message);
    if (confirm == true) {
        // User said yes
    } else {
        // User said no
    }
}
</script>

Edit Ah, took another look at your implementation.

Here is what you can do to achieve the result you're looking for:

<input type="submit" name="none" text="test" onClick="showMessage(<?php echo $titulo ?>, <?php echo $contenido ?>, <?php echo $tipo ?>)">
<script>
  showMessage(title, text, tip) {
    if (tip == 'ask') {
      swal({
        title: title,
        text: text,
        type: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#DD6B55',
        confirmButtonText: 'Yes, delete it!',
        cancelButtonText: 'No, cancel plx!',
        closeOnConfirm: false,
        closeOnCancel: false
      },
      function(isConfirm){
        if (isConfirm) {
          return true;
        } else {
          return false;
        }
      });
    }
  }
</script>

I still recommend you look up AJAX and the purpose of it. It will help you wrap your head around the full stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM