0

I have a button in my php page :

<button id="myButton">Delete me</button>

and in that page I have a variable which I want to pass to a JavaScript function, and this is my JS code :

<script>
        $(function() {
            $('#myButton').confirmOn('click', function(e, confirmed){
                if(confirmed) {
                    //Here I'll use the variable
                }

            })

        });

</script>

How can I do that ?

Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191

4 Answers4

0

Assuming you're talking about passing php variables to Javascript, you can do this when writing to the page, ex:

<?php

$passThis = 'Passing'

?>

<script language="javascript" type="text/javascript">
    var sStr = "My name is <?php echo $passThis ?>.";

    document.write(sStr);
</script>

You could also get integer values, doing something like

$integerValue = 5;
var int = "<?php echo $integerValue; ?>";
int = parseInt(int);

By modifying this, you could use it to pass more types of variables, so assuming you have something like this:

<?php
    $text = 'someText';
?>

<script>
        $(function() {
            $('#myButton').confirmOn('click', function(e, confirmed){
                if(confirmed) {
                    //Here I'll use the variable
                }

            })

        });

</script>

you could do

<script>
        $(function() {
            $('#myButton').confirmOn('click', function(e, confirmed){
                if(confirmed) {
                    console.log("<?php echo $text ?>");
                }

            })

        });

</script>

to make Javascript alert 'someText'.

Cilan
  • 13,101
  • 3
  • 34
  • 51
0
 <button id="myButton">Delete me</button>
 <input type="hidden" name="variable" id="variable" value=2>

 <script>
    $(function() {
        $('#myButton').confirmOn('click', function(e, confirmed){
            if(confirmed) {=
                alert(document.getElementById('variable').value);
//Here I'll use the variable
            }

        })

    });

Omicans
  • 531
  • 1
  • 8
  • 26
0

You can declare the variable outside the click event like so:

$(function() {
  var confirmed = true;

  $('#MyButton').confirmOn('click', function() {
    if(confirmed) {
      // do stuff
    }
  });
});
Ozziereturnz
  • 488
  • 4
  • 8
0

I think you might be wanting to put a variable through PHP on your button and pass it to your function using jQuery data. Check this out:

<button data-confirmed="<?php echo $confirmed; ?>" id="myButton">Delete me</button>

And in your js:

        $(function() {
            $('#myButton').on('click', function(e){
                // get jquery object access to the button
                var $thisButton = $(this);

                // this gets that data directly from the HTML
                var confirmed = $thisButton.data('confirmed');

                if(confirmed) {
                    //Here I'll use the variable
                }

            })
        });

Basically, you can access variables in javascript using this method if you are interpolating PHP vars directly on the page. If this isn't what you are looking for, please let me know in comments.

Evan
  • 5,975
  • 8
  • 34
  • 63