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!

Jim22150
  • 511
  • 2
  • 8
  • 22
  • 1
    You should look to use Ajax. – Debflav Jul 22 '14 at 08: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 '14 at 08: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 '14 at 08:50
  • @Debflav I'm just trying to learn about PHP `sessions`. – Jim22150 Jul 22 '14 at 08:52
  • @Jim22150 I believe it is not working. At least without refreshing the page. When page is rendered, `php` job is done. What you see in your browser and what `php` does are in fact somehow isolated things. `AJAX` is the tool that you're looking for to join them. – ex3v Jul 22 '14 at 08:53
  • 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 '14 at 08:55
  • @ex3v ok, could you toss up a quick example of your ajax solution please? I use AJAX as much as possible but I am making a PHP CRUD and am having trouble querying certain PHP variables via frontend. – Jim22150 Jul 22 '14 at 08:57
  • @DarkBee thanks, this is what I was afraid of. JS it is then. – Jim22150 Jul 22 '14 at 09:02
  • 1
    @Jim22150 [here](http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php) – ex3v Jul 22 '14 at 09:03

1 Answers1

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

Pierre
  • 558
  • 1
  • 7
  • 36