-2

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

这是我的代码:

<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;

}
4

4 回答 4

2

这是我的方法

 <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>
于 2013-09-07T09:39:36.037 回答
0

try replacing your function with javascript ajax call

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");}
     });

}
于 2013-09-07T07:21:04.963 回答
-1

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;

    }
于 2013-09-07T07:21:04.877 回答
-1

do u know how php page is evaluated on server.

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.

It can be sent in two ways.

  1. By Page reloading

  2. By AJAX.

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

<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/

于 2013-09-07T07:21:40.277 回答