如何从PHP变量中的onClick获取值

<form method="post" action="#">
name <input type="text" name="na" />
Male <input type="radio" name="r" value="Male" />
Female <input type="radio" name="r" value="FeMale" />
<input type="submit" name="sbt" onclick="return confirm('are sure');"  />
</form>

</body>
</html>

<?php
$a ="";
if(isset($_POST['sbt']))
{
    echo $a=
}

?>

I want to get value true or false from ONCLICK function in $a variable of PHP.

You can't get result of it inside PHP variable $a without posting it to script, you need to use jQuery or equivalent to post value to server and receive answer and then modify your html code or do anything you want

you can try with ajax and if return success - submit your form with post param ?

Use the AJAX instead, You can not do with Simple Javascript. Capture and send the boolean value via Ajax params. Follow this url: http://thisinterestsme.com/simple-ajax-request-example-jquery-php/ will help you out of this.

Hope this helps!

this is input field and the javascript try like this way:

function confirm_func(product_image_id, product_id) {
var r = confirm("Are you sure?");
if (r == true) {
    $.ajax({
        url: '/your file.php',
        method: "POST",
        {
            product_id: product_id
        },
        success: function (data) {

        }
    });
}

}

and check is isset POST['product_id'] .... Hope this helps!

Is this something you are looking for? Made with simple javascript function.

<form method="post" action="#">
name <input type="text" name="na" />
Male <input type="radio" name="r" value="Male" />
Female <input type="radio" name="r" value="FeMale" />
<input type="hidden" id="confirm" name="confirm" value="">
<input type="submit" name="sbt" onclick="confirmTrueOrFalse()"  />
</form>


<script type="text/javascript">
function confirmTrueOrFalse()
{
var x;
var r=confirm("Are you sure?");
if (r==true){
  document.getElementById("confirm").value = "true";
  }
else{
  document.getElementById("confirm").value = "false";

  }
}
</script>

<?php
$a ="";
if(isset($_POST['sbt']))
{
    $a = $_POST['confirm'];
    echo $a;
}

?>