0

I am trying to implement this example on my website: http://jsfiddle.net/eDeQr/1/ The problem is that I need the $cp1 = 4;variable to be global and not inside that function. As soon as I do that the function doesn't give any output in the second field.

This is what I want it to look like:

<script type="text/javascript">
    function calc() {
    var textValue1 = document.getElementById('input1').value;
    var textValue2 = $cp1;

    document.getElementById('output').value = textValue1 * textValue2;
};
</script>

The $cp1 variable should be global.

user3604070
  • 61
  • 1
  • 5

1 Answers1

2

You're mixing JavaScript and PHP. You actually have to echo out the PHP variable inside of your JavaScript. Otherwise the JavaScript is looking for a JavaScript variable named $cpl which is undefined.

var textValue2 = <?php echo $cp1; ?>
John Conde
  • 217,595
  • 99
  • 455
  • 496