简体   繁体   中英

I want to generate a bool value when I click a button

I want to execute some scripts only when the user confirms.
Sample code like this

if 'confirm' is clicked, then $confirm = true;   

if($confirm)
 {
   some script
 }
else
 {
   other script
 }
 ?>

there are two buttons 'confirm' and 'discard'. If confirm is clicked then $confirm = true;

Say you have two buttons like this

<input type="button" name="confirm" id="confirm" value="confirm">
<input type="button" name="discard" id="discard" value="discard">

You can use jquery event handlers for getting which button is clicked like this

$(document).ready(function() {
   $("#confirm").on('click',function(){
     alert('Confirm clicked');
     //If you need to communicate with the server the do an ajax call here
   });

   $("#discard").on('click',function(){
     alert('Discard clicked');
     //If you need to communicate with the server the do an ajax call here
   });
});

You can check for the click event reference from HERE

You can do with php, extract state of fields with environment php variables, you just make an formular and send some data to your webserver script-file.

<?php
$confirm = $_POST["name_of_the_button"]
if(isset($confirm))
 {
   some script
 }
else
 {
   other script
 }

-----EDIT-----:

I made it with two files:

File: form.php

<html>
<head>
<title>Button test</title>
<script type="application/javascript">
function ajaxObj()
{
   var xmlHttp=null;
   try
   {
      // Browsers Firefox, Opera 8.0+, Safari;
      xmlHttp=new XMLHttpRequest();
   }
   catch(e)
   {
      try
      {
         // Browser Internet Explorer;
         xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch(e)
      {
         try
         {
            // Other browsers
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
         catch(e)
         {
            // Your browser does not suport XMLhttp object
            alert('Your browser does not suport XMLhttp object');
            return false;
         }
      }
   }
   return xmlHttp;
}

function doQuery(id)
{
    xmlHttp=ajaxObj();
    if(xmlHttp==null)
    {
        alert('Your browser does not suport XMLhttp object');
        return;
    }
    xmlHttp.open("POST","check.php",true);
    var send = 'id='+id;
    xmlHttp.onreadystatechange=function()
    {
        if (xmlHttp.readyState==4 && xmlHttp.status==200)
        {
            document.getElementById("output").innerHTML=xmlHttp.responseText;
        }
    }
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlHttp.send(send);
}
</script>
</head>
<body>
<button id="confirmed" name="button" onclick="doQuery(this.id)">Confirm</button>
<button id="declined" name="button" onclick="doQuery(this.id)">Decline</button>
<div id="output"></div>
</body>
</html>

File check.php:

<?php
$action = $_POST["id"];
$confirm = false;
if($action=="confirmed")
    $confirm = true;
else if($action=="declined")
    $confirm = false;
else {}
?>

if You use javascript then use belo:-

if(window.confirm("please confirm"))
{
    return true;
    // or do Your operation.
}
else{
    return false;
    // or do Your operation.
}

otherwise You can use jquery.

<input type="button" name="confirm" id="confirm" value="confirm"> 
<input type="button" name="discard" id="discard" value="discard">

$(document).ready(function() {    
  $("input:button").on('click',function(){
    if($(this).id == 'confirm'){ 
      // put your script here
    }else{ 
      // put your script here }   
    });

});

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