Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

This question already has an answer here:

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 ?>
share|improve this question

marked as duplicate by Quentin, tkone, Elliott Frisch, chopper, Charlie Kilian Mar 21 '14 at 19:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers 3

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

share|improve this answer
    
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

the PHP code executes on the server side and the JacaScript 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.

share|improve this answer

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>
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.