1

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);

        }
savagenoob
  • 415
  • 9
  • 25

2 Answers2

2

You are setting $repid but echoing $repname

Scuzzy
  • 12,186
  • 1
  • 46
  • 46
0

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;?>';
                    }
savagenoob
  • 415
  • 9
  • 25