0

According to the main acceptance of the title, how do you explain the following:

       ...
       $temp1=$_POST['expert_id']; ?>
       <script type="text/javascript">
          var jstemp1 =<?php echo json_encode($temp1); ?>;
       </script>
       <?php         

       $temp1=$_POST['answers_id']; ?>
       <script type="text/javascript">
          var jstemp2 =<?php echo json_encode($temp1); ?>;
       </script>

Suppose $_POST['expert_id']=1 and $_POST['answers_id']=2. My thought is that $temp1 will equal to 2 when Javascript code begins to execute since PHP code executes first. Therefore, jstemp1 would equal to 2 and jstemp2 would equal to 2. However, to my big surprise, jstemp1=1 and jstemp2=2. Can you explain that to me?

3
  • Because you've reassigned the value of the PHP variable $temp1 from $_POST['expert_id'] (presumably 1) to $_POST['answers_id'] (presumably 2). Commented Jul 10, 2015 at 10:34
  • You did not see my point. When Javascript begins to run, $temp1=2, isn't it? Commented Jul 10, 2015 at 10:39
  • Nope - imagine you were writing all your JavaScript from PHP with echo statements; echo <script type... blah blah blah> ... </script> you're still pushing the code to the output buffer (screen) but the JS won't be executed as it's output (same as with just loading an normal HTML page) Commented Jul 10, 2015 at 10:54

3 Answers 3

2

Strip out all the JavaScript and you should see what's going on server side; you are literally doing:

$temp1=$_POST['expert_id'];
echo json_encode($temp1);

$temp1=$_POST['answers_id'];
echo json_encode($temp1);

So if $_POST['expert_id'] = 1 and $_POST['answers_id'] = 2 ...

$temp1=1;
echo json_encode($temp1);
// outputs 1

$temp1=2;
echo json_encode($temp1);
// outputs 2

So what you'll have client-side is:

<script type="text/javascript">
    var jstemp1 =1;
</script>

<script type="text/javascript">
    var jstemp2 =2;
</script>

All the server-side code (PHP) executes before the client-side code (JavaScript)

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

Comments

1

Your PHP code is inline with HTML/Javascript it will be executed first on the server, true,
but the evaluation is be done in the sequence you have wrote it.

You assign a value to a variable, then print it out.
After that you assign a different value to the same variable, over-writing it, then print it out.

This is perfectly normal.

Comments

0

PHP executes the code in server side before the page is sended to the browser.

The browser receives:

<script type="text/javascript">
  var jstemp1 =1;
</script>
<script type="text/javascript">
  var jstemp2 =2;
</script>

And when the page is loaded javascript code is executed.

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.