0

I have some implicit PHP functions which are to be called on particular front-end events; such as button onclicks. By doing so this should modify my PHP SESSION variable accordingly. However, both functions, setAlan() and setMark(), seem to be getting called even if I just want the first one to execute.

<?php
    session_start();

    function setAlan() {
        $_SESSION['passed_user']='alan';
    }
    function setMark() {
        $_SESSION['passed_user']='mark';
    }
?>

<script>
    function getAlan() { $(document.body).append('<?php setAlan(); ?>'); }
    function getMark() { $(document.body).append('<?php setMark(); ?>'); }
    function show() { alert(' <?php echo "{$_SESSION['passed_user']}" ?> '); }
</script>

<div class="pod">
    <div class="pod_title"><h2>Select a User</h2></div>
    <div>
        <input type="button" onclick="getAlan();" value="alan">
        <input type="button" onclick="getMark();" value="mark">
        <input type="button" onclick="show();" value="show">
    </div>
</div>

I don't understand why setMark() is being called automatically? By what I've written, I believe I am only defining the PHP functions, not calling them; I use JS for that. Am I missing something? Thanks in advance!

9
  • 1
    You should look to use Ajax. Commented Jul 22, 2014 at 8:47
  • PHP is executed server side. Appending PHP on the client side will not work. U'll have to use ajax for this to work. Commented Jul 22, 2014 at 8:48
  • @DarkBee Umm, it is working, i believe. I just want to implement a conditional based on each of the buttons... Any solution for this? Commented Jul 22, 2014 at 8:50
  • 1
    U think its working because PHP is parsed BEFORE any output is sent to the user. If u check your source code u will see that function getAlan() appends an empty string to the source because the PHP in it is already parsed and executed thus making the session having a value of mark Commented Jul 22, 2014 at 8:55
  • 1
    @Jim22150 here Commented Jul 22, 2014 at 9:03

1 Answer 1

1

PHP is executed first, server side. Once PHP is done, your browser ( client ) gets the result, Javascript is executed on the client, so after PHP is done.

You can't mix javascript and PHP as you did with:

<script>
  function getAlan() { $(document.body).append('<?php setAlan(); ?>'); }
  function getMark() { $(document.body).append('<?php setMark(); ?>'); }
  function show() { alert(' <?php echo "{$_SESSION['passed_user']}" ?> '); }
</script>

However you can use Javascript to call some server side PHP script with ajax ( Asynchronous JavaScript and XML ).

http://www.w3schools.com/ajax/default.ASP

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.