0

Please help, I would like to get using PHP the get_id variable declared inside the javascript function.

<script>
     function confirmDialog (id, callback) {
        document.getElementById ("idConfirmDialogPrompt").innerHTML = id;
        confirmDialogCallback = callback;
        $("#idConfirmDialog").modal ("show");
        var get_id = id;
        }
</script>

          <?php echo "ID  : ". get_id ?>
user3068522
  • 143
  • 1
  • 2
  • 7

3 Answers3

1

PHP code executes on the server side and JavaScript code executes on the client side.

Because the server side code executes before the client side code, the server side code cannot use references to the client side code and variables.

luchaninov
  • 6,792
  • 6
  • 60
  • 75
Fisch
  • 3,775
  • 1
  • 27
  • 38
0

Javascript is client side, PHP is server-side. PHP can give information to javascript, but javascript can't give information to PHP (directly).

echolocation
  • 1,120
  • 1
  • 10
  • 28
  • does it mean it's impossible to get info from javascript? – user3068522 Mar 21 '14 at 17:21
  • You can use ajax to call a seperate php script, much like you would post information to that script, but otherwise, no you cannot get info from js like you are attempting here. – echolocation Mar 21 '14 at 17:23
0

So, I don't know if there's a way to do that, but if you just want to print the "get_id" on screen, you could do this:

<script>
     function confirmDialog (id, callback) {
        document.getElementById ("idConfirmDialogPrompt").innerHTML = id;
        confirmDialogCallback = callback;
        $("#idConfirmDialog").modal ("show");

        document.getElementById("result").innerHTML = id;
        }
</script>

<div id="result"></div>

I think it'll work anyway.

You also could send the value of "get_id" to other PHP page to process the value.

<script>
     function confirmDialog (id, callback) {
        document.getElementById ("idConfirmDialogPrompt").innerHTML = id;
        confirmDialogCallback = callback;
        $("#idConfirmDialog").modal ("show");
            window.location.href = 'page.php?id='+id;
        }
</script>
Alan D.
  • 89
  • 4