Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to pass a PHP variable to a Javascript function but it keeps displaying as 'undefined'. I have the Javascript on a separate page and am including it on the page that I am calling the function from. Here is the code:

<?php $repname    = $_SESSION['REPNAME']; ?>
<script type="text/javascript" src="js/chat.js"></script>

<input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" onclick="javascript:sendChatText('<?php echo $repname;?>');" />

Here is js/chat.js:

function sendChatText(repname) {
    alert(repname);

        }
share|improve this question
Have you called session_start() yet? – Kemal Fadillah Jan 23 '12 at 2:47
Yeah, I echo the variable successfully – savagenoob Jan 23 '12 at 2:48
Whats wierd is if I change the call to onclick="javascript:sendChatText("My Name"); it still says undefined. somethings wrong on the javascript side. – savagenoob Jan 23 '12 at 7:52

2 Answers

You are setting $repid but echoing $repname

share|improve this answer
Oops, I am trying to only display relevant code and copied wrong variable – savagenoob Jan 23 '12 at 2:41
so the problem is still outstanding? If so double check you're not getting a 404 on your JS link. – Scuzzy Jan 23 '12 at 3:23
yeah still outstanding.... no 404.. – savagenoob Jan 23 '12 at 3:25
Honestly I can't see a problem with your code. You don't have any quotes inside the string that need escaping? – Scuzzy Jan 23 '12 at 22:42

I still don't know why this isn't working but I got around it by adding that Javascript function to the PHP page and doing this:

var repName;
        function sendChatText() {
        repName = '<?php echo $repname;?>';
                    }
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.