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.

1
  • Just define the variable outside of the function: jsfiddle.net/eDeQr/2 Commented Jun 3, 2014 at 23:41

1 Answer 1

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; ?>
Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick, thanks for the help! I will mark this as soon as I am allowed to.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.