Consider this simple example;

<?php $text = test; ?>

<script type="text/javascript" defer="defer">

var test;

test = "<?php echo $text; ?>"

$(document).ready(function(){
alert(test);
});

</script>

This works fine. Creating the alert with the text from the php var. However, if I place;

<?php $text = test; ?>

below the script - it does not work. I've tried the defer function. What am I doing wrong?

Cheers

share|improve this question

What exactly do you want to do? Are you trying to get the value of the JavaScript variable test in PHP? – Felix Kling Jun 2 '11 at 12:12
but where do you want to put the php variable? if you put it before the alert or in the alert method then it should work... – Cata Jun 2 '11 at 12:12
Note that <?php $text = test; ?> is wrong in any case unless you have a constant test. You should set a proper string: <?php $text = 'test'; ?>. If you think you are referring to the JavaScript variable test if you put the line at the end, then you are wrong. See @Quentin's answer in this case. – Felix Kling Jun 2 '11 at 12:19
1  
The problem here is very clear, but in general, please do not ever write "it does not work". "It does not work" is completely meaningless, and consequently highly irritating. – Lightness Races in Orbit Jun 2 '11 at 12:23
All I'm trying to do is pass PHP variables to javascript. Since my javascript code is defined in the <head> and my PHP variables obviously come after this in my code - I thought there may have been a simple solution. Thanks for all replies/help – steve Jun 2 '11 at 12:27
show 1 more comment
feedback

3 Answers

up vote 6 down vote accepted

If you place

  <?php $text = "test"; ?> 

below the JS code, the the variable $text is not defined yet, so you cannot echo it earlier (edit) in the script.

share|improve this answer
What do you mean $text isn't defined? That code defines it! (Well, it tries to do so, but since assigning test will error (unless you've defined a constant called test or something), it breaks). Actually, in retrospect, the whole question doesn't make a lot of sense as it claims things work even though they should error. – Quentin Jun 2 '11 at 12:14
@Quentin: If there is no constant test, then PHP interprets it as string 'test'. That's why it works if this line is put before <?php echo $text; ?> but obviously it would not echo anything if it was put after it (this is what the OP tried). It is only unclear what the OP really wants to do (your answer also makes sense, given the ambiguity of test). In any way, the downvote is unjustified (whoever did this). – Felix Kling Jun 2 '11 at 12:16
Read the second half of his post. He mentions moving the $test definition to BELOW the javascript. So, when echo $test happens, $test is not defined. – Scott Harwell Jun 2 '11 at 12:17
Wow. PHP really has kept the very worst bits of Perl. Take my previous comments as being unaware of PHP's unquoted strings feature. – Quentin Jun 2 '11 at 12:18
1  
@Quentin: To be fair, you do get an E_NOTICE for it. It shouldn't be done. – Lightness Races in Orbit Jun 2 '11 at 12:25
show 2 more comments
feedback

Seems like you are trying to assign a client-side variable to a server-side variable?

Due to my knowledge, server-side variables can NOT "interact" directly with client-sider variables without anything in between. This means <?php $test = test; ?> doesn't work properly since variables that are included in <?php ?> will be treated as server-side variables, and thus, your client-side variable test is either considered as

  • an undefined constant, or
  • a string without quotes ''
share|improve this answer
feedback

There are two computers involved, the web server (which processes php) and the user's browser (which processes javascript). So no, you cannot transfer a javascript variable back to php without sending the value of that variable from the user's browser to the web server (normally done by posting a form or ajax).

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
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.