1

I am trying to use javascript's confirm function to set my php variable $continue based on the confirm click. I feel like the code below should work, but $continue is always false.

    <script type="text/javascript">
    <!--
    var answer;
    answer = confirm("Do you want to continue?");
    if (answer) { <?php $continue = true; ?> }
    else { <?php $continue = false; ?> };

    // -->
    </script>

Any help is always appreciated!

3
  • 1
    Pay attention that PHP works on server-side whereas JavaScript on the client-side. The only option is to send/receive data to/from PHP script. Commented May 13, 2012 at 1:16
  • Oh.. so the two $continue expressions are evaluated before the javascript executes? Commented May 13, 2012 at 1:18
  • Definitely. Even before the page is shown in your browser. Commented May 13, 2012 at 1:19

1 Answer 1

4

php is server side, javascript is client side.

if you need to change something via javascript do this using ajax. but you need keep this in your mind: the php just write html and process http requests. all of the interaction in the client side who will change some state in the server must perform a http request.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.