-1

why does the following code not work?

<?php
$j= "<option value='kj' name='dd'>" ;
?>

<script type="text/javascript">
    function getinfo(a){     
    if(a==2){
    var k ="<?php echo $j;?>";
    alert(k);
    }


}
</script>

It works when I say $j="option";

17
  • 1
    Any errors? Have you checked your page's generated source? What does it say in there? Commented Sep 7, 2012 at 18:57
  • Well it does not show the alert box so I assume it's not working Commented Sep 7, 2012 at 18:57
  • 2
    You need to debug it. Is your function even being called? Do you have any errors in your console? What does your generated source show? Commented Sep 7, 2012 at 18:58
  • 1
    @sam no you won't, in the context of putting a value in a JS variable from PHP, you don't need to do any deserialization in the JS. It will be the value as-is. Commented Sep 7, 2012 at 19:08
  • 1
    @sam I'm sorry, I don't understand where deserialization would ever be necessary in this. Can you show me an example? Commented Sep 7, 2012 at 19:34

1 Answer 1

4

There are a couple possibilities here:

1) getInfo is never called.

2) a is never equal to 2

3) There are errors in other javascript code on your page.

EDIT 4): Other people have pointed out that the content of $j may not be allowed in an alert box.

There is no way for anyone here to tell which it is.

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

1 Comment

+1 W/o seeing how/if getInfo is called, this is unanswerable.

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.