Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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变量_Php_Javascript - Fatal编程技术网

如何在javascript中获取php变量

如何在javascript中获取php变量,php,javascript,Php,Javascript,我想通过javascript获取captchaCode.php文件中的$security_code的值,在captcha.php文件中。我不能将此文件包含在captcha.php文件中。以下是Captcha_code.php的代码,此代码不包含在此文件的任何函数中: $captcha = new CaptchaCode(); $security_code = str_encrypt($captcha->generateCode(6)); 这是我的javascript函数,通过这个函数我想

我想通过javascript获取captchaCode.php文件中的$security_code的值,在captcha.php文件中。我不能将此文件包含在captcha.php文件中。以下是Captcha_code.php的代码,此代码不包含在此文件的任何函数中:

$captcha = new CaptchaCode();
$security_code = str_encrypt($captcha->generateCode(6));
这是我的javascript函数,通过这个函数我想得到$security的值:

function refresh_captcha()
{
    var img = document.getElementById('captcha_img');
    img.src = '<?php echo "/captcha_images.php"?>';
    jQuery("#captcha_img").attr("src",img.src);
    jQuery("#security_code").val("<?php echo $security_code;?>");
}
函数刷新\u验证码()
{
var img=document.getElementById('captcha_img');
img.src='';
jQuery(“#captcha_img”).attr(“src”,img.src);
jQuery(“#安全代码”).val(“”);
}

实际上,此代码用于在刷新captcha而不刷新页面时获取新的加密值。

您必须更改与图像源分配相关的代码。不需要分配PHP标签。只需像这样指定PHP文件名

function refresh_captcha()
{
    var img = document.getElementById('captcha_img');
    img.src = '/captcha_images.php';
    jQuery("#captcha_img").attr("src",img.src);
    /*This line will not work without Ajax request*/
    /*jQuery("#security_code").val("<?php echo $security_code;?>");*/
}
函数刷新\u验证码()
{
var img=document.getElementById('captcha_img');
img.src='/captcha_images.php';
jQuery(“#captcha_img”).attr(“src”,img.src);
/*如果没有Ajax请求,该行将无法工作*/
/*jQuery(“#安全代码”).val(“”)*/
}

在php文件captcha.php中,

    $captcha = new CaptchaCode();
    $security_code = str_encrypt($captcha->generateCode(6));?>
    <script>
         var code = '<?= $security_code ?>';
    </script><?php // rest of the php codes

向页面发送ajax请求以仅输出以下代码:

文件:outputcode.php(仅限演示)

接下来,使用AJAX获取该值,例如jQuery

$("#refresh_code").on('click', function() {
   $.get("outputcode.php", function(data) {
      //data will now hold the new code
   });
});

希望这能给你一个主意。

如果你不能包含这些文件,你需要使用ajax请求

captcha.php

$captcha = new CaptchaCode();
$security_code = str_encrypt($captcha->generateCode(6));
echo json_encode($security_code);
在您的js中:

<img src="" id="captcha_img" onclick="refresh_captcha()"/>
<script>

    function refresh_captcha()
{
    $.ajax({
        url : 'captcha.php',
        type : 'POST',
        dataType : 'json',
        success : function (security_code) {
            var source = security_code;
            var img = document.getElementById('captcha_img');
            img.src = source;
            jQuery("#captcha_img").attr("src",img.src);
            jQuery("#security_code").val(source);
        }
    });

}
</script>

函数刷新\u验证码()
{
$.ajax({
url:'captcha.php',
键入:“POST”,
数据类型:“json”,
成功:功能(安全代码){
var source=安全代码;
var img=document.getElementById('captcha_img');
img.src=源;
jQuery(“#captcha_img”).attr(“src”,img.src);
jQuery(“#安全代码”).val(源代码);
}
});
}
img上的onclick事件可能是完全错误的,但出于锻炼的目的,我把它放在了那里



取决于$security\u代码是什么,例如,您可以不使用JSON。

您需要使用ajax。为什么需要用php输出静态字符串?为什么不直接用JavaScript编写字符串呢?变量应该存在于当前文件中,所以您需要包含它。否则,您必须将请求发送到.php文件并使用它给出的答案。您有两种选择:使用Ajax获取php值,或者使用Cookies。您可以为我提供Ajax代码吗..我不知道怎么做this@itachi,否,javascript无法与PHP通信。它将只具有最初接收到的第一个值。@Starx erm。。。。在这里,javascript不需要做任何事情。随机性将通过
php
,而不是
javascript
@itachi?????我真的明白你的意思。javascript在这里怎么不起作用呢?对captcha_images.php的请求只会发出一次,不是吗?我错了。我以为OP只提到了验证码图像。在这种情况下,ajax是过火了,否则,ajax是必须的。如果要更改图像,使用随机字符串更改img src属性就足够了。它的给定错误是未定义$。如果我使用jQuery代替$then,则会显示其给出的错误,即jQuery未定义…@user2746093,您需要首先包括jQuery库。现在它不会刷新验证码图像。它应该如何刷新?在本例中,它会在单击图像时刷新。我单击图像但它不会刷新captchait它会更改src属性,就我所能测试的而言,如果它不会更改实际图像,那么应该还有另一个问题。是否有其他方法获取php变量javascript?
$captcha = new CaptchaCode();
$security_code = str_encrypt($captcha->generateCode(6));
echo json_encode($security_code);
<img src="" id="captcha_img" onclick="refresh_captcha()"/>
<script>

    function refresh_captcha()
{
    $.ajax({
        url : 'captcha.php',
        type : 'POST',
        dataType : 'json',
        success : function (security_code) {
            var source = security_code;
            var img = document.getElementById('captcha_img');
            img.src = source;
            jQuery("#captcha_img").attr("src",img.src);
            jQuery("#security_code").val(source);
        }
    });

}
</script>