PHP:从 javascript 重新加载验证码图像

发布于 2024-07-22 04:37:14 字数 421 浏览 0 评论 0原文

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

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

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

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

请指导我解决这个问题

提前致谢

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

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 ?

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

Please guide me to solve this

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2024-07-29 04:37:14

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

  • javascript 使用 XHR 但这可能很痛苦。 这里是一个示例。
  • jQuery 提供了一种简单的方法来执行此操作,例如:

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

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

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

如果您的注册页面返回验证错误,则无需使用 javascript 来重新加载验证码,这应该会自动发生。

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

  • 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');
    });

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文