Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 全局php变量不';不影响函数的输出_Javascript_Php_Variables_Global Variables - Fatal编程技术网

Javascript 全局php变量不';不影响函数的输出

Javascript 全局php变量不';不影响函数的输出,javascript,php,variables,global-variables,Javascript,Php,Variables,Global Variables,我正在尝试在我的网站上实现此示例: 问题是我需要$cp1=4变量为全局变量,不在该函数内。一旦我这样做,函数就不会在第二个字段中给出任何输出 这就是我希望它看起来的样子: <script type="text/javascript"> function calc() { var textValue1 = document.getElementById('input1').value; var textValue2 = $cp1; document.g

我正在尝试在我的网站上实现此示例: 问题是我需要
$cp1=4
变量为全局变量,不在该函数内。一旦我这样做,函数就不会在第二个字段中给出任何输出

这就是我希望它看起来的样子:

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

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

函数计算(){
var textValue1=document.getElementById('input1').value;
var textValue2=$cp1;
document.getElementById('output')。value=textValue1*textValue2;
};

$cp1
变量应该是全局变量。

您混合了JavaScript和PHP。实际上,您必须回显JavaScript中的PHP变量。否则,JavaScript将查找名为
$cpl
的JavaScript变量,该变量未定义

var textValue2 = <?php echo $cp1; ?>
var textValue2=

只需在函数外部定义变量:这就成功了,谢谢您的帮助!一旦我被允许,我会尽快做标记。