javascript 如何在不刷新页面的情况下刷新验证码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18670774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:49:42  来源:igfitidea点击:

How to refresh captcha code without refreshing the page

phpjavascripthtml

提问by user2746093

When I click on my given refresh link, my captcha code refreshes only once. If I again click on the refresh link it doesn't refresh the captcha code. I don't understand why that is happening.

当我点击给定的刷新链接时,我的验证码只刷新一次。如果我再次单击刷新链接,它不会刷新验证码。我不明白为什么会这样。

This is my code:

这是我的代码:

<a href='javascript: refresh_captcha();'>refresh</a>  
function refresh_captcha()
{
    <?php
        $captcha1 = new CaptchaCode();
        $code = str_encrypt($captcha1->generateCode(6));

    ?>
    var img = document.getElementById('captcha_img');
    img.src = '<?php echo "/captcha_images.php?width=120&height=40&code=$code"?>';
    document.getElementById ("captcha_img").src = img.src;

}

回答by Shushant

here is my approach

这是我的方法

 <p>
      <img id='captcha_img' style='border: 1px solid #CBD8E5;' src='/captcha.php?img=<?=time();?>'/><br/>

      <a href="#" onclick="document.getElementById('captcha_img').src='captcha.php?img=' + Math.random(); return false">Reload Captcha</a>
 </p>

回答by sudhAnsu63

try replacing your function with javascript ajax call

尝试用 javascript ajax 调用替换你的函数

function refresh_captcha()
{
    $.ajax({
    type:"GET"
    url: "/captcha_images.php?width=120&height=40&code=$code"?>'
    success: function(msg)
   {
    document.getElementById ("captcha_img").src = msg;
   },
error:functon(){alert("some error occured");}
     });

}

回答by developerCK

do u know how php page is evaluated on server.

你知道如何在服务器上评估 php 页面吗?

means:

方法:

when a php page is parsed on server , then php code is evaluated and and evaluated result is send back to page .then page is sent to browser.my mean to say, you will have only static content as html javascript, css on your browser.if you want to evaluate your php code then you have to send a request to server.

当在服务器上解析 php 页面时,将评估 php 代码并将评估结果发送回页面。然后将页面发送到浏览器。我的意思是说,您的浏览器上只有 html javascript、css 等静态内容.如果您想评估您的 php 代码,那么您必须向服务器发送请求。

It can be sent in two ways.

它可以通过两种方式发送。

  1. By Page reloading

  2. By AJAX.

  1. 通过页面重新加载

  2. 通过 AJAX。

so if you don't want to reload you page then you should go for ajax. then you code will be

因此,如果您不想重新加载页面,那么您应该选择 ajax。那么你的代码将是

<a href='javascript: refresh_captcha();'>refresh</a>  
function refresh_captcha()
{
 // ajax call to server to get new captcha string
 // evaluate this code on server and send string back to browser
 //<?php
 //   $captcha1 = new CaptchaCode();
 //   $code = str_encrypt($captcha1->generateCode(6));
 //
 //?>
 // var code = <ajax response>

var img = document.getElementById('captcha_img');
img.src = '/captcha_images.php?width=120&height=40&code='+code; // change this line
//document.getElementById ("captcha_img").src = img.src;

}

for ajax call read this http://www.w3schools.com/ajax/

对于 ajax 调用,请阅读此 http://www.w3schools.com/ajax/

回答by Rajiv Ranjan

Use below code

使用下面的代码

<a href='javascript: refresh_captcha();'>refresh</a>  
    function randomString(length, chars) {
        var result = '';
        for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
        return result;
    }
    function refresh_captcha()
    {
        var code = randomString(6, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
        var img = document.getElementById('captcha_img');
        img.src = '<?php echo "/captcha_images.php?width=120&height=40&code="?>'+code;
        document.getElementById ("captcha_img").src = img.src;

    }