Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

What I want do is echo some JavaScript and use PHP variables in the JavaScript code I'm echoing.

Here's what i have tried so far.

echo "<script>document.getElementById('1').innerHTML = $output;</script>";
echo "<script>document.getElementById('1').innerHTML = '$output';</script>";
echo "<script>document.getElementById('1').innerHTML = ".$output.";</script>";
echo "<script>document.getElementById('1').innerHTML = '.$output.';</script>";
echo '<script>document.getElementById("1").innerHTML = "$output";</script>';
echo "<script>document.getElementById('1').innerHTML =".'$output'.";</script>";

I'm out of ideas, what am I doing wrong here?

share|improve this question
3  
Doing random things in programming very rarely leads to the expected result. At first, do you realize what you want to see as a result? Show the expected result string here. – zerkms Apr 10 '13 at 3:17
    
Do you realize what my question is? All i asked is how to use a PHP variable in there. The result is irrelevant because my script works if i remove the PHP variable and replace it by a string. – Frederic Daniel Apr 10 '13 at 3:24
    
Just a side note you really should not mix Javascript with PHP like this... – Dave Apr 10 '13 at 3:24
    
@Frederic Daniel: I do realize what your question is about. And you don't realize what the result should be, thus you're trying something random things. It doesn't work in programming. Anyway, we don't care - there are tons of lazy people like you here who cannot program and don't even want to learn. – zerkms Apr 10 '13 at 3:33
up vote 2 down vote accepted

Simple, just (ab)use json_encode(); it works pretty well on strings too:

?>
<script>
document.getElementById('1').innerHTML = <?php echo json_encode($output); ?>;
</script>
share|improve this answer
echo "<script>document.getElementById('1').innerHTML = '".$output."'</script>";
share|improve this answer

Try something like this instead..

echo "<script type='text/javascript'>";
echo "document.getElementById('1').innerHTML = '".$output."';";
echo "document.getElementById('1').innerHTML = '".$output."';";
echo "document.getElementById('1').innerHTML = '".$output."';";
echo "document.getElementById('1').innerHTML = '".$output."';";
echo "document.getElementById('1').innerHTML = '".$output."';";
echo "</script>";

This is how you echo javascript with PHP. This still doesn't do anything (except repeatedly assign $output the element 1's inner html), but I assume you know that.

Also, you need to be careful that $output doesn't contain anything that will mess with your Javascript, like a single quote.

share|improve this answer
<script>
document.getElementById('1').innerHTML = "<?php echo $output ?>";
</script>
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.