Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.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
Php 通过ajax和jquery重新加载外部页面onLinkClick_Php_Jquery_Ajax_Captcha - Fatal编程技术网

Php 通过ajax和jquery重新加载外部页面onLinkClick

Php 通过ajax和jquery重新加载外部页面onLinkClick,php,jquery,ajax,captcha,Php,Jquery,Ajax,Captcha,这是链接 <a href="javascript:loadCaptcha();">Reload</a> 我想单击它并通过ajax和jquery加载一个外部文件。在我的页面标题中,我有以下javascript代码 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script language="javascri

这是链接

<a href="javascript:loadCaptcha();">Reload</a>

我想单击它并通过ajax和jquery加载一个外部文件。在我的页面标题中,我有以下javascript代码

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
    $(function(){
        loadCaptcha();
    });
    function loadCaptcha()
    {
        $.ajax({
            type: 'POST',
            url: 'captcha.php',
            data: "",
            cache: false,
            success: function(res){
                $('#captcha').html(res);
            }
        });
    }
</script>

$(函数(){
loadCaptcha();
});
函数loadCaptcha()
{
$.ajax({
键入:“POST”,
url:'captcha.php',
数据:“,
cache:false,
成功:功能(res){
$('#captcha').html(res);
}
});
}
我想在点击该链接时重新加载captcha.php。Onpageload文件captcha.php正在id#captcha中加载-但在该链接上单击它不重新加载

php代码

<?php
$capt = generate_captcha();
echo '<img src="captcha.jpg" id="imgcaptcha" name="imgcaptcha" alt="'.$capt.'" style="max-width: 110px; max-height: 35px;" title="Captcha"/><input type="hidden" name="captchacode" id="captchacode" value="'.$capt.'" />';

function generate_captcha()
    {
        $number= rand(11111, 99999);
        $width = 110;
        $height= 35;
        //create image using imagecreate php function
        $img = imagecreate($width, $height);
            $backcolor = imagecolorallocatealpha(
                      $img,
                      hexdec( substr( 'FFFFFF', 0, 2 ) ),
                      hexdec( substr( 'FFFFFF', 2, 2 ) ),
                      hexdec( substr( 'FFFFFF', 4, 2 ) ),
                      127 * ( 100 - 100 ) / 100
                    );
        imagefill($img, 0, 0, $backcolor);
            //create line color
        $linescolor = imagecolorallocatealpha(
                      $img,
                      hexdec( substr( '333333', 0, 2 ) ),
                      hexdec( substr( '333333', 2, 2 ) ),
                      hexdec( substr( '333333', 4, 2 ) ),
                      127 * ( 100 - 70 ) / 100
                    );
        //generate different lines  
        for( $i=0; $i<10; $i++ ) {
            imageline($img, mt_rand(0,110), mt_rand(0,35),
             mt_rand(0,110), mt_rand(0,35), $linescolor);
            }
        //Font file path
        $font_path = "jokerman.ttf";
        //create color for font
        $font_color = imagecolorallocatealpha(
                      $img,
                      hexdec( substr( '000000', 0, 2 ) ),
                      hexdec( substr( '000000', 2, 2 ) ),
                      hexdec( substr( '000000', 4, 2 ) ),
                      127 * ( 100 - 100 ) / 100
                    );
        //add characters to the image
        imagettftext($img, 22, 0, 5, 28, $font_color, $font_path, $number);
        //store image
        imagejpeg($img, "captcha.jpg", 100);
        return $number;
    }
?>

更新:chrome显示更新的图像,firefox和ie不更新图像。缓存问题。我没有通过php头使用缓存,但运气不佳。


请告知。

使用
onclick
而不是
href=“javascript:loadCaptcha();”

重新加载

重新加载
$(文档)。在(“单击”,“重新加载验证码”,加载验证码);

由于缓存问题,IE和Firefox将不会刷新验证码图像。解决方法是每次删除验证码图像并创建具有唯一名称的新验证码图像。由于名称将是唯一的,验证码将被刷新,FF将重新加载图像,因为它是新的


我已经通过每次重新创建唯一的验证码图像并在重新加载时调用该唯一图像解决了此问题。

尝试通过jqueryTried绑定单击事件,现在…仍然没有重新加载。我认为您的问题在别处…是的,我相信是这样。我可以加载文件,但我不能使用相同的函数通过链接重新加载它。您能提供captcha.php中的代码吗?是的。谢谢captcha.php code added.UPDATE:验证码图像正在更改,但更改后的图像不会显示。重新加载时,旧图像会保留在那里,但如果我验证了它,它会验证无效,因为它已更改…它只是不刷新。如何在单击“重新加载”时刷新它。
<a onclick="loadCaptcha();">Reload</a>
<a id="reloadCaptcha">Reload</a>

<script type="text/javascript">
 $(document).on("click", "#reloadCaptcha", loadCaptcha);
</script>