PHP:从javascript重新加载验证码图片 [英] PHP: Reloading the captcha image from javascript

查看:47
本文介绍了PHP:从javascript重新加载验证码图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中有一个用户注册表格。我在页面中放入了验证码图像检查。我像这样使用它

I have a user registration form in PHP .I put captcha image check in the page.I used it like this

<img src="captcha.php" alt="Enter this text in the adjacent text box" id="imgCaptcha" />

,在我的JavaScript中,我将使用图像中生成的相同数字(来自验证码)对此进行验证.php页面)。该数字也在会话变量中设置。现在,如果验证失败,我想将图像从javascript重新加载到另一个图像。有什么办法吗?

and in my javascript i will validate this with the same number which is generated in the image (from captcha.php page) . That number was set in a session variable too. Now i want to reload the image to another image from my javascript , if the validattion fails .Is there any way to do so ?

在我的captcha.php页面中,我我随机创建一个数字,然后使用imagejpeg创建图像。我还将这个数字(在图片中)也设置为会话变量

In my captcha.php page,I am creating a Number randomly and then creating an image using imagejpeg. I set this number (in the image) to as session varible also

请引导我解决这个问题

预先感谢

推荐答案

您需要将 captcha.php 注入到页面使用ajax。有几种方法可以做到这一点:

You need to inject captcha.php into the page using ajax. There are several ways to do this:


  • 使用XHR的javascript,但这会很痛苦。 此处是一个示例。

  • jQuery 提供了一种简单的方法,例如:

  • javascript using XHR but this can be a pain. Here is an example.
  • jQuery provides a simple way to do this, for example:

$('#someButton')。click(function(){
$('#someDiv')。load('captcha.php');
});

$('#someButton').click(function() { $('#someDiv').load('captcha.php'); });

prototype.js 有点冗长,但是可以完成工作。 示例

prototype.js is a bit more verbose, but does the job. Example.

我建议使用jQuery,文档中有很棒的例子

I would suggest using jQuery, there are great examples in the docs.

如果您的注册页面返回验证错误,则无需使用Java验证码重新加载验证码,那应该自动进行。

If your registration page returns on a validation error, you do not need to use javascript for the captcha to get reloaded, that should happen automatically.

这篇关于PHP:从javascript重新加载验证码图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆