Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
1  
You should look to use Ajax. –  Debflav Jul 22 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. –  DarkBee Jul 22 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? –  Jim22150 Jul 22 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 –  DarkBee Jul 22 at 8:55
1  
@Jim22150 here –  ex3v Jul 22 at 9:03

1 Answer 1

up vote 1 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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